Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if any elements in one list are in another

Tags:

python

list

I'm trying to compare two lists and simply print a message if any value from the first list is in the second list.

def listCompare():
  list1 = [1, 2, 3, 4, 5]
  list2 = [5, 6, 7, 8, 9]
  if list1 in list2:
    print("Number was found")
  else:
    print("Number not in list")

In this example, I want the if to evaluate to True because 5 is in both lists. This doesn't work, and I'm not sure of the simplest way to compare the two lists.

like image 296
h1h1 Avatar asked Apr 22 '13 01:04

h1h1


People also ask

How do you check if items in list are in another list?

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.")

How do you see if an item in a list is in another list Python?

To check if the item exists in the list, use Python “in operator”. For example, we can use the “in” operator with the if condition, and if the item exists in the list, then the condition returns True, and if not, then it returns False.

How do you know if two elements are present in a list?

Check if list1 contains any elements of list2 using any() Python any() function checks if any Element of given Iterable is True. So, convert the list2 to Iterable and for each element in Iterable i.e. list2 check if any element exists in list1.


5 Answers

You could solve this many ways. One that is pretty simple to understand is to just use a loop.

def comp(list1, list2):     for val in list1:         if val in list2:             return True     return False 

A more compact way you can do it is to use map and reduce:

reduce(lambda v1,v2: v1 or v2, map(lambda v: v in list2, list1)) 

Even better, the reduce can be replaced with any:

any(map(lambda v: v in list2, list1)) 

You could also use sets:

len(set(list1).intersection(list2)) > 0 
like image 70
David Alber Avatar answered Sep 18 '22 15:09

David Alber


There are different ways. If you just want to check if one list contains any element from the other list, you can do this..

not set(list1).isdisjoint(list2) 

I believe using isdisjoint is better than intersection for Python 2.6 and above.

like image 36
vidit Avatar answered Sep 21 '22 15:09

vidit


Your original approach can work with a list comprehension:

def listCompare():
  list1 = [1, 2, 3, 4, 5]
  list2 = [5, 6, 7, 8, 9]
  if [item for item in list1 if item in list2]:
    print("Number was found")
  else:
    print("Number not in list")
like image 34
dansalmo Avatar answered Sep 17 '22 15:09

dansalmo


There is a built in function to compare lists:

Following is the syntax for cmp() method −

cmp(list1, list2)

#!/usr/bin/python

list1, list2 = [123, 'xyz'], [123, 'xyz']

print cmp(list1,list2)

When we run above program, it produces following result −

0

If the result is a tie, meaning that 0 is returned

like image 38
user1463110 Avatar answered Sep 18 '22 15:09

user1463110


You could change the lists to sets and then compare both sets using the & function. eg:

list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]

if set(list1) & set(list2):
    print "Number was found"
else:
    print "Number not in list"

The "&" operator gives the intersection point between the two sets. If there is an intersection, a set with the intersecting points will be returned. If there is no intersecting points then an empty set will be returned.

When you evaluate an empty set/list/dict/tuple with the "if" operator in Python the boolean False is returned.

like image 34
colins44 Avatar answered Sep 19 '22 15:09

colins44