list1= [{'name': 'a'}, {'name': 'b'}, {'name': 'c'}] list2 = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}] for l1 in list1: if l1['name'] есть ли в list2 == False: print(str(l1['name']) +' удален') 

who will advise what?

    3 answers 3

    Using Pandas :

     In [275]: import pandas as pd In [276]: d1 = pd.DataFrame.from_records(list1) In [277]: d2 = pd.DataFrame.from_records(list2) In [278]: d1 Out[278]: name 0 a 1 b 2 c In [279]: d2 Out[279]: id name 0 1 a 1 2 b In [280]: pd.merge(d1, d2, on='name', how='outer', indicator=True) Out[280]: name id _merge 0 a 1.0 both 1 b 2.0 both 2 c NaN left_only 

    Here are some more options:

     In [299]: from operator import itemgetter In [300]: set1 = set([itemgetter('name')(c) for c in list1]) In [301]: set1 Out[301]: {'a', 'b', 'c'} In [302]: [c for c in list2 if c['name'] in set1] Out[302]: [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}] In [303]: set([itemgetter('name')(c) for c in list1]) - set([itemgetter('name')(c) for c in list2]) Out[303]: {'c'} 
    • In [302]: [c for c in list2 if c ['name'] in set1] Out [302]: [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}], only here the links remain - user284883
     In [48]: list1= [{'name': 'a'}, {'name': 'b'}, {'name': 'c'}] ...: list2 = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}] ...: In [53]: for i in list1: ...: for j in list2: ...: if i['name'] in j.values(): ...: print(f'Значения из {i} есть в {j}') ...: Значения из {'name': 'a'} есть в {'id': 1, 'name': 'a'} Значения из {'name': 'b'} есть в {'id': 2, 'name': 'b'} 

    UPD:

     In [75]: vals = [x['name'] for x in list2] In [76]: for i in list1: ...: if i['name'] not in vals: ...: list1.remove(i) ...: In [77]: list1 Out[77]: [{'name': 'a'}, {'name': 'b'}] 
    • it turns out right now how many dictionaries I have, so many times he tries to delete from me - user284883
    • @ user284883 it is possible after print() to put break - Pavel Durmanov
    • @ user284883 edited the answer according to question changes - Pavel Durmanov
    • list1.remove (i) does not work in this case - user284883
    • @ user284883 why? - Pavel Durmanov pm
     list1= [{'name': 'a'}, {'name': 'b'}, {'name': 'c'}] list2 = [{'id': 1, 'name': 'a'}, {'id': 2, 'name': 'b'}] list2_set = set([itemgetter('name')(c) for c in list2]) list = [c for c in list1 if c['name'] not in list2_set] for l in list : print(l['name'] + 'удален') 

    thanks MaxU, pushed

     vals = [x['name'] for x in list2] for i in list1: if i['name'] not in vals: print(i['name'] + 'удален') 

    Thanks Alban, this way is 80% longer

    • All the same, the obvious is better than the implicit, my answer is much more readable :) - Pavel Durmanov
    • I study 1 month, I agree about readability, but here there is a large 15 thousand dictionaries. this is just one of the streams, I have three of them. thanks to your answers, little by little comes understanding. thanks - user284883
    • @ user284883, you can indicate in the question what do you want to get at the output? - MaxU 7:07 pm
    • MaxU thanks, wrote already, very helpful - user284883