-5

I have two lists :

a = [{'id':1,'qty':2,'name':x},{'id':2,'qty':5,'name':b}]
b = [{'id':1,'name':x , 'barcode': 1578563445},{'id':2,'name':b , 'barcode': 9856754}]

I want to make a list or dictionary like below :

c= [
    {'id':1,'qty':2,'name':x,'barcode': 1578563445 },
    {'id':2,'qty':5,'name':b ,'barcode': 9856754 }
]
pault
  • 41,343
  • 15
  • 107
  • 149
  • 1
    How do you want to handle conflicts? For instance, if the dict with id `3` has different values for `barcode` in the two different input lists? – Jonathan Scholbach Oct 31 '19 at 15:20
  • 2
    Possible duplicate of [join two lists of dictionaries on a single key](https://stackoverflow.com/questions/5501810/join-two-lists-of-dictionaries-on-a-single-key) – pault Oct 31 '19 at 15:22

5 Answers5

0

you can do :

a = [{'id':1,'qty':2,'name':'x'},
    {'id':2,'qty':5,'name':'b'}]
b= [{'id':1,'name':'x' , 'barcode': 1578563445},
    {'id':2,'name':'b' , 'barcode': 9856754}]


for elem_b in b:
    for elem_a in a:
        if elem_a['id'] == elem_b['id']:
            elem_a.update(elem_b)

output:

[{'id': 1, 'qty': 2, 'name': 'x', 'barcode': 1578563445}, {'id': 2, 'qty': 5, 'name': 'b', 'barcode': 9856754}]
Alexall
  • 423
  • 2
  • 12
0
c = []
for d in a:
    u = [x for x in b if x['id'] == d['id']]
    for i in u:
        d.update(i)
    c.append(d)
ATK7474
  • 339
  • 2
  • 12
0
a = [{'id':1,'qty':2,'name':x},{'id':2,'qty':5,'name':b}]
b = [{'id':1,'name':x , 'barcode': 1578563445},{'id':2,'name':b , 'barcode': 9856754}]    

for x in b:
    c = [z.update(x) for z in a if z['id'] == x['id']]  

print(a)

dict.update() explained.

GSazheniuk
  • 1,340
  • 10
  • 16
0

This might not be the most efficient solution, but I think that it's a fairly simple and intuitive implementation.

c = []
for entry in a + b:

    # Loop through c to check if a matching id exists
    found = False
    for j in range(len(c)):
        if c[j]['id'] == entry['id']:

            # update all fields if id matches
            for k in entry.keys():
                c[j][k] = entry[k]
                found = True  # Set flag to not add new entry
            break
    if not found:  # Only append if we did not find an existing match
        c.append(entry)

return c

I'm not sure how you want to handle conflicting information, such as if there are two different barcodes for the same ID. If that will likely be an issue, this will need some additional clauses to handle that situation.

ByrnesW
  • 33
  • 1
  • 7
-1

You can combine both dicts in a list comprehension using zip. Also your example data is missing quotes around the names.

a = [{'id':1,'qty':2,'name':'x'},{'id':2,'qty':5,'name':'b'}]
b = [{'id':1,'name':'x' , 'barcode': 1578563445},{'id':2,'name':'b' , 'barcode': 9856754}]

c = [dict({*a1.items(), *b1.items()}) for a1, b1 in zip(a,b)]

The value of c would be:

[{'name': 'x', 'id': 1, 'barcode': 1578563445, 'qty': 2}, {'qty': 5, 'id': 2, 'barcode': 9856754, 'name': 'b'}]
Jab
  • 26,853
  • 21
  • 75
  • 114