Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine a list with dictionaries and values outside to allow for loop

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading