0

I have 2 lists of strings that I need to compare.

Say list1 is ["a", "b", "c", "d"] and list2 is ["a", "b", "c", "d", "e", "f"] I have succeded in comparing them with

check = any(i in ccourt for i in champs)
print(check) 

Edit: Ok so I asked poorly and misunderstood the any() method, sorry, what I'm stuck on is the comparing part. Yes the any() part works, but it doesn't say that the "e" and "f" of list2 isn't in list1, that is what I want it do to.

Thank you in advance

bk_
  • 751
  • 1
  • 8
  • 27
  • `if not check`? – Sayse Dec 21 '20 at 11:24
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [ask] from the [tour]. "Teach me this basic language feature" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a _specific_ question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – TigerhawkT3 Dec 21 '20 at 11:24
  • See [this](https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python) post for how to raise an exception in Python. – costaparas Dec 21 '20 at 11:25

2 Answers2

2

You can use "set":

set(list2) - set(list1)

result:

set(['e', 'f']) # printed output

with set.difference it would also work.

set(list1).difference(list2)

ZF007
  • 3,708
  • 8
  • 29
  • 48
1
lst1 = ["a", "b", "c", "d"]

list2 = ["a", "b", "c", "d", "e", "f"]

for i in list2:
    if i not in lst1:
        print(i, 'is not in lst1')

>>> e is not in lst1
>>> f is not in lst1

With map:

list(map(lambda i: print(i, 'is not in lst1') if i not in lst1 else None, list2))

>>> e is not in lst1
>>> f is not in lst1
coderoftheday
  • 1,987
  • 4
  • 7
  • 21