using Python, I want to check if a list contains an item/value that is also present in another list. For example, here is what I am trying to do:
list1 = ['item1','item2','item3']
list2 = ['item4','item5','item3']
if list1 contains any items also in list2:
print("Duplicates found.")
else:
print("No duplicates found.")
As you can see, list2 contains an item that is also found in list1, which is 'item3'. Is there any way I can detect if this occurs?
Thanks.
Using any()
along with a generator expression:
list1 = ['item1','item2','item3']
list2 = ['item4','item5','item3']
if any(x in list1 for x in list2):
print("Duplicates found.")
else:
print("No duplicates found.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With