How to append dictionary to a list in loop

2020-07-20 03:24发布

问题:

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']?

回答1:

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:

my_list = [('a1', 'b1', 'c1', 'd1', 'e1'), ('a2', 'b2', 'c2', 'd2', 'e2')]
keys = ('key1', 'key2', 'key3', 'key4', 'key5')
final = [dict(zip(keys, elems)) for elems in my_list]

After that, the value of the final variable is:

>>> final
[{'key3': 'c1', 'key2': 'b1', 'key1': 'a1', 'key5': 'e1', 'key4': 'd1'},
{'key3': 'c2', 'key2': 'b2', 'key1': 'a2', 'key5': 'e2', 'key4': 'd2'}]

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.:

>>> final[0]['key1']
'a1'


回答2:

Use zip to combine a pre-defined list of key names with each tuple in your input list, then pass the results to dict to make them into dicts. Wrap the whole thing in a list comprehension to process them all in one batch:

keys = ('key1', 'key2', 'key3', 'key4', 'key5')
finalDictList = [dict(zip(keys, values)) for values in tupleList]


回答3:

I'm not sure I see why you need to convert everything to a dictionary, when you've already got a list of tuples.

>>> tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
...              ('a2', 'b2', 'c2', 'd2', 'e2'),
...              ('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]
>>> [x[1] for x in tupleList]
['b1', 'b2', 'b10000']

Using Python's list comprehension syntax, you can get a list of all the n-th elements of each tuple.



回答4:

If the fields are fix you can do this:

fields = ['key1', 'key2', 'key3', 'key4', 'key5']

newList = [dict(zip(fields, vals)) for vals in oldList]


回答5:

If as you say you have a lot of entries, remember that python has namedtuples:

>>> tupleList = [('a1', 'b1', 'c1', 'd1', 'e1'),
...              ('a2', 'b2', 'c2', 'd2', 'e2'),
...              ('a10000', 'b10000', 'c10000', 'd10000', 'e100000')]
>>>
>>> from collections import namedtuple
>>> fv = namedtuple('fivevals', ('key1', 'key2', 'key3', 'key4', 'key5'))
>>> tuplelist = [fv(*item) for item in tupleList]
>>> 
>>> tuplelist[0].key1
'a1'
>>>

Namedtuples can be accesed by key but at the same time they are lightweight and require no more memory than regular tuples.



回答6:

from itertools import izip

keys = ['key1', 'key2', 'key3', 'key4', 'key5']
finalDictList = [dict(izip(names, x)) for x in tupleList]


回答7:

finalDictList = []
for t in tupleList:
    finalDictList.append({
        'key1': t[0],
        'key2': t[1],
        'key3': t[2],
        'key4': t[3],
        'key5': t[4],
    })

Also, if that is possible, can we access each dictionary by it's key values, say, like finalDictList[0]['key1']?

Absolutely, that is exactly how you would do it.