how to convert from list of lists to a dictionary
Hi :) I have this table and I need to convert it into a dictionary where the firsl line are the keys and then each value from the rest of the tables will be values but iterating through them each at the time . I have this table :
my_table = [["Account Manager", "Name", "BP", "Contact", "Quote Name", "Type", "Status", "Total"],
["bubble", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
["rose", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
["purple", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
["dark", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
["star", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
["asmr", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],]
This is the code used to iterate through it :
dict_from_table = {}
for position, key in enumerate(my_table[0]):
dict_from_table[key] = []
for value_row in my_table[1:2]:
dict_from_table[key].append(value_row[position])
print(dict_from_table)
And this is the output that comes from it :
{'Account Manager': ['bubble'], 'Name': ['kwlak'], 'BP': ['5677'], 'Contact': ['conn'], 'Quote Name': ['987'], 'Type': ['butter'], 'Status': ['fish'], 'Total': ['fill']}
I need it to go through all the lists and return each value once for the keys (Account manager , name , etc.) but also I do not want it to return the value as a list I want it to be in the same dictionary as the keys. Do I need another for loop ?
You are nested loop is inverted. Since your keys are fixed, build an empty dictionary from those keys with empty lists as values:
In [7]: my_table = [["Account Manager", "Name", "BP", "Contact", "Quote Name", "Type", "Status", "Total"],
...: ["bubble", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
...: ["rose", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
...: ["purple", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
...: ["dark", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
...: ["star", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],
...: ["asmr", "kwlak", "5677", "conn", "987", "butter", "fish", "fill", "stat", "total"],]
...:
In [8]: keys = my_table[0]
In [9]: result = {k:[] for k in keys}
Then loop over the rest of the table:
In [10]: for row in my_table[1:]:
...: for k,v in zip(keys, row):
...: result[k].append(v)
...:
In [11]: result
Out[11]:
{'Account Manager': ['bubble', 'rose', 'purple', 'dark', 'star', 'asmr'],
'Name': ['kwlak', 'kwlak', 'kwlak', 'kwlak', 'kwlak', 'kwlak'],
'BP': ['5677', '5677', '5677', '5677', '5677', '5677'],
'Contact': ['conn', 'conn', 'conn', 'conn', 'conn', 'conn'],
'Quote Name': ['987', '987', '987', '987', '987', '987'],
'Type': ['butter', 'butter', 'butter', 'butter', 'butter', 'butter'],
'Status': ['fish', 'fish', 'fish', 'fish', 'fish', 'fish'],
'Total': ['fill', 'fill', 'fill', 'fill', 'fill', 'fill']}
Although note, this assumes that your keys in the first row line up with the rest... the example data does not have that, but I assume this is just a transcription error.
You can use zip
to 'transpose' the table (switch rows and columns):
output = {column[0]: column[1:] for column in zip(*my_table)}
(Or use list(column[1:])
if you need a list instead of tuple.)
However if you are doing a lot of manipulation on table data, consider using pandas
. You can create a dataframe like this:
import pandas as pd
df = pd.DataFrame(my_table[1:], columns=my_table[0])
then export it back to a dict of lists like this:
output = df.to_dict("list")
(You will need to make sure there are the same number of column headings as cells in each row first.)