how to get a dictionary out of a list
I'm trying to get a dictionary out of a list . First I had a list of lists and with the code below I got a list from the list of lists :
y_table = [["Account Manager", "Name", "BP", "Contact", "Quote Name", "Type", "Status", "Total"],
["bubble", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],
["rose", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],
["purple", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],
["dark", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],
["star", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],
["asmr", "kwlak", "5677", "conn", "987", "butter", "fish", "fill"],]
flat_list = []
for sublist in my_table:
for item in sublist:
flat_list.append(item)
print(flat_list)
now my list of lists looks like this :
['Account Manager', 'Name', 'BP', 'Contact', 'Quote Name', 'Type', 'Status', 'Total', 'bubble', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill', 'rose', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill', 'purple', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill', 'dark', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill', 'star', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill', 'asmr', 'kwlak', '5677', 'conn', '987', 'butter', 'fish', 'fill']
Now I want from this list to get a dictionary which I want it to look like this at the end :
Table: [{'Account Manager': 'bubble', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'rose', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'purple', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'dark', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'star', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'asmr', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn', 'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'}]
I tried to run this code to get this dictionary but it is not working :
header_my_table = flat_list[0]
my_table.remove(flat_list[0])
my_table_dict = dict(zip(header_my_table, flat_list))
print(my_table_dict)
It seems it's much easier to construct your desired list from y_table
instead of flattening it. Since you want to use the first sublist as keys and the other sublists as values, you can simply iterate over y_table[1:]
and use dict constructor on zipped pairs of y_table[0]
and each sublist:
out = [dict(zip(y_table[0], lst)) for lst in y_table[1:]]
Output:
[{'Account Manager': 'bubble', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'rose', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'purple', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'dark', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'star', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'},
{'Account Manager': 'asmr', 'Name': 'kwlak', 'BP': '5677', 'Contact': 'conn',
'Quote Name': '987', 'Type': 'butter', 'Status': 'fish', 'Total': 'fill'}]