Combining multiple lists
4 answers
You can iterate over the elements of the dictionary at each iteration and check if the key is id1
or id2
, then store the value:
[j for d in l_data for i,j in d.items() if i=='id1' or i=='id2']
Alternatively, you can also use operator.itemgetter
with a function map
:
>>> data = [ {'id1':1, 'col1':'va1', 'id2':1001}, {'id1':2, 'col1':'va2', 'id2':1002}]
>>> map(itemgetter('id1','id2'),data)
[(1, 1001), (2, 1002)]
+3
source to share