I have at my disposal huge amount of data, in the form of a list of tuples. Each tuple has a specified format like (a, b, c, d, e)
. The list of tuples looks like:
tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
('a2', 'b2', 'c2', 'd2', 'e2'),
...
('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]
What I want is, to convert each of these tuples to a dictionary, and append the dictionary to a a final list of dictionaries. Can all this be done in a loop? The final list of dictionaries should look like:
finalDictList = [{'key1': 'a1', 'key2': 'b1', 'key3': 'c1', 'key4': 'd1', 'key5': 'e1'},
{'key1': 'a2', 'key2': 'b2', 'key3': 'c2', 'key4': 'd2', 'key5': 'e2'},
{'key1': 'a3', 'key2': 'b3', 'key3': 'c3', 'key4': 'd3', 'key5': 'e3'},
...
{'key1': 'a10000', 'key2': 'b10000', 'key3': 'c10000', 'key4': 'd10000', 'key5': 'e10000'}]
The format of the tuples is fixed. I want to compare afterwords, value of each key of a dictionary with all others. This is why the conversion of tuple to dictionary made sense to me. Please correct me if the design paradigm itself seems wrong. Also, there are >10000 tuples. Declaring that many dictionaries is just not done.
Is there anyway to append dictionary to a list in a loop? Also, if that is possible, can we access each dictionary by it's key values, say, like finalDictList[0]['key1']
?
Use
zip
to combine a pre-defined list of key names with each tuple in your input list, then pass the results todict
to make them into dicts. Wrap the whole thing in a list comprehension to process them all in one batch:Absolutely, that is exactly how you would do it.
We're going to mix three important concepts to make this code really small and beautiful. First, a list comprehension, then, the zip method, and finally, the dict method, to build a dictionary out of a list of tuples:
After that, the value of the
final
variable is:Also, you can get elements of a certain dictionary using the position of the dictionary in the list and the key you're looking for, i.e.:
I'm not sure I see why you need to convert everything to a dictionary, when you've already got a list of tuples.
Using Python's list comprehension syntax, you can get a list of all the n-th elements of each tuple.
If as you say you have a lot of entries, remember that python has namedtuples:
Namedtuples can be accesed by key but at the same time they are lightweight and require no more memory than regular tuples.