+

Python | Check if two lists have any common element

Method # 1: using any()

# Python code to check for two lists
# have some common element

 
# List initialization

list1 = [ 1 , 2 , 3 , 4 , 55 ]

list2 = [ 2 , 3 , 90 , 22 ]

 
# using any function

out = any (check in list1 for check in list2)

 
# Check status

if out:

print ( "True"

else :  

print ( "False" )

Exit :

 True 

Method # 2: Using in operator.

# Python code to check if there are two lists
# which one -or generic element

 
# List initialization

list1 = [ 1 , 3 , 4 , 55 ]

list2 = [ 90 , 22 ]

 

flag = 0

 
# Used to validate an element
# second list to first list

for elem in list2:

if elem in list1:

  flag = 1

 
# checking status

if flag = = 1 :

print ( " True "

else :

print ( "False" )

Output:

 False 
Get Solution for free from DataCamp guru