iterating over two lists to find differences but result shows all values

  Kiến thức lập trình

I have these two lists:

listA = [{'IP': '1.1.1.1', 'var': 'test1'}, {'IP': '2.2.2.2', 'var': 'test2'}, {'IP': '3.3.3.3', 'var': 'test4'}]
listB = ['1.1.1.1', '2.2.2.2', '3.3.3.3', '4.4.4.4', '5.5.5.5', '6.6.6.6', '7.7.7.7']

This is my code:

not_exist = []
for A in listA:
    for B in listB:
        if B != A['IP'] and B not in not_exist:
            not_exist.append(B)

But this is what I have currently:

>>> not_exist
['2.2.2.2', '3.3.3.3', '4.4.4.4', '5.5.5.5', '6.6.6.6', '7.7.7.7', '1.1.1.1']

I’m going to find which IPs don’t exist in listA but exist in listB.

Expected output:

>>> not_exist
['4.4.4.4', '5.5.5.5', '6.6.6.6', '7.7.7.7']

1

LEAVE A COMMENT