简体   繁体   中英

how to create a list of dictionaries from nested lists

I have a header list for the 'keys' of my dictionary and a nested list which I want to use as 'values' and I want to return a list of dictionaries. But when I iterate over the nested list and header list and append the dictionaries to a list, I am only getting the last dictionary. What am I doing wrong here?

rows = [['Branden',27,'M'],['Casey',22,'F'],['Paul',30,'M']]
header = ['Name','Age','Gender']
d = {}
data = []
for item in rows:
    for key,value in zip(header,item):
      d[key] = value
data.append(d)

data

Output I am getting:

[{'Age': 30, 'Gender': 'M', 'Name': 'Paul'},
 {'Age': 30, 'Gender': 'M', 'Name': 'Paul'},
 {'Age': 30, 'Gender': 'M', 'Name': 'Paul'}]

My Desired Output:

[{'Name': 'Branden', 'Age': 27, 'Gender': 'M'}, 
{'Name': 'Casey', 'Age': 22, 'Gender': 'F'}, 
{'Name': 'Paul', 'Age': 30, 'Gender': 'M'}]

What am I doing wrong here?

dict takes an iterable of tuples in interprets these as key-value pairs. So all you have to do is to call dict with zip(headerrow, next_row) (pseudo-code!) as the argument for all your rows.

>>> header = ['key1', 'key2', 'key3']
>>> values = [['a', 'b', 'c'], ['d', 'e', 'f']]
>>> dicts = [dict(zip(header, v)) for v in values]
>>> dicts
[{'key3': 'c', 'key2': 'b', 'key1': 'a'}, {'key3': 'f', 'key2': 'e', 'key1': 'd'}]

You are not creating new dictionaries for each item. Move d = {} to be after the line for item in rows: .

you need to create a new dictionary per sublist in rows, and append it to data as below:

rows = [['Branden',27,'M'],['Casey',22,'F'],['Paul',30,'M']]
header = ['Name','Age','Gender']
data = []
for sublist in rows:
    d = {} #new dictionary per sublist
    for i,element in enumerate(sublist):
        # i is the iteration number, beginning with 0
        d [header[i]] = element
    data.append(d)
print (data)

prints (on one line):

[{'Gender': 'M', 'Age': 27, 'Name': 'Branden'}, 
{'Gender': 'F', 'Age': 22, 'Name': 'Casey'}, 
{'Gender': 'M', 'Age': 30, 'Name': 'Paul'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM