Issue
It's hard for me to explain in the title, but I have a list of 2 dictionaries data
, and I want to insert things
and stuff
into them according to index via for loop or other iterators.
this is what I have:
things = ['7121703099311426821', '7114869117433154821', ]
stuff = ['fjfueirjk', 'aoiwhef', ]
data = [{'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]
And I want them to be like:
data = {'id1':7121703099311426821, 'stuff: 'fjfueirjk', 'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'id1':7114869117433154821, 'stuff': 'aoiwhef', 'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}
So that I can say
for x in data:
print(x['id1'])
print(x['stuff'])
print(x['data1'])
# do stuff
Solution
One approach, IIUC:
res = [{ "id" : thing, "stuff" : s, **d} for thing, s, d in zip(things, stuff, data)]
print(res)
Output
[{'id': '7121703099311426821', 'stuff': 'fjfueirjk', 'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'id': '7114869117433154821', 'stuff': 'aoiwhef', 'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]
Answered By - Dani Mesejo Answer Checked By - Timothy Miller (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.