Reputation: 11
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?
Upvotes: 1
Views: 682
Reputation: 1975
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'}]
Upvotes: 0
Reputation: 1763
You are not creating new dictionaries for each item. Move d = {}
to be after the line for item in rows:
.
Upvotes: 0
Reputation: 78650
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'}]
Upvotes: 3