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

Time:07-20

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

CodePudding user response:

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}]

CodePudding user response:

I recommend using enumerate and zip:

things = ['7121703099311426821', '7114869117433154821', ]
stuff = ['fjfueirjk', 'aoiwhef', ]

data = [{'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]\

for idx, item in enumerate(zip(things, stuff)):
    data[idx]["id1"] = item[0]
    data[idx]["stuff"] = item[1]
    
print(data)

Output

[{'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000, 'id1': '7121703099311426821', 'stuff': 'fjfueirjk'}, {'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814, 'id1': '7114869117433154821', 'stuff': 'aoiwhef'}]

This allows you to explicitly see where the new entries in data are being created.

CodePudding user response:

Basic implementation

things = ['7121703099311426821', '7114869117433154821', ]
stuff = ['fjfueirjk', 'aoiwhef', ]
data = [{'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]

new_data = []

if len(things)==len(stuff) and len(things)==len(data): # Just to be sure, that we don't get an out of bounds error
    for i in range(len(things)):
        element = {}
        element[f'id{i 1}'] = things[i]
        element['stuff'] = stuff[i]
        for k,v in data[i].items():
            element[k] = v
        new_data.append(element)

data = new_data
print(data)

Output

[{'id1': '7121703099311426821', 'stuff': 'fjfueirjk', 'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'id2': '7114869117433154821', 'stuff': 'aoiwhef', 'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]

CodePudding user response:

In python >= 3.9, You can merge two dict like dict1 | dict2.

out = [{ "id" : thing, "stuff" : s} | d for thing, s, d in zip(things, stuff, data)]
# ------^^^^^^^^^^dict1^^^^^^^^^^^--^dict2^
print(out)

[
    {'id': '7121703099311426821', 'stuff': 'fjfueirjk', 'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, 
    {'id': '7114869117433154821', 'stuff': 'aoiwhef', 'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}
]

CodePudding user response:

If you don't need that particular order of keys inside the dicts:

things = ['7121703099311426821', '7114869117433154821', ]
stuff = ['fjfueirjk', 'aoiwhef', ]
data = [{'data1': 1009, 'data2': 52, 'data3': 43, 'data4': 45000}, {'data1': 115, 'data2': 7, 'data3': 9, 'data4': 1814}]

for d, d['id1'], d['stuff'] in zip(data, things, stuff):
    pass

import pprint
pprint.pp(data)

Output (Try it online!):

[{'data1': 1009,
  'data2': 52,
  'data3': 43,
  'data4': 45000,
  'id1': '7121703099311426821',
  'stuff': 'fjfueirjk'},
 {'data1': 115,
  'data2': 7,
  'data3': 9,
  'data4': 1814,
  'id1': '7114869117433154821',
  'stuff': 'aoiwhef'}]
  • Related