How to convert a list of data in Python to a dictionary where each element has a key

Google={}
Google["Price"]=[317.68,396.05,451.48,428.03,516.26,604.83,520.63,573.48,536.51,542.84,533.85,660.87,728.9]

I have a Google dictionary, where the key value displays 36 values ​​for Google. Is there a way to give each record a separate key (where 317.68 is 1, 396.05 is 2, etc.)?

+4
source share
2 answers
dict(enumerate(google_price_data, start=1))
+6
source

Just use enumerateto help your key generation task and forscroll through each item in the list.

Here you go:

google_dict = dict()
google_price_data = [317.68,396.05,451.48,428.03,516.26,604.83,520.63,573.48,536.51,542.84,533.85,660.87,728.9]

for i, item in enumerate(google_price_data, start=1):
    google_dict[i] = item

print google_dict

Output:

{
    1: 317.68,
    2: 396.05,
    3: 451.48,
    4: 428.03,
    5: 516.26,
    6: 604.83,
    7: 520.63,
    8: 573.48,
    9: 536.51,
    10: 542.84,
    11: 533.85,
    12: 660.87,
    13: 728.9
}
+4
source

Source: https://habr.com/ru/post/1616888/


All Articles