Write a program to check 2 lists and find if any element is common


Topic: Write a program to check 2 lists and find if any element is common

Solution

  
def common_data(list1, list2): 
    result = False
  
    for x in list1: 
  
        # traverse in the 2nd list 
        for y in list2: 
    
            # if one common 
            if x == y: 
                result = True
                return result  
                  
    return result 
      
a = [1, 2, 3, 4, 5] 
b = [5, 6, 7, 8, 9] 
print(common_data(a, b)) 
  
a = [1, 2, 3, 4, 5] 
b = [6, 7, 8, 9] 
print(common_data(a, b)) 



List all Python Programs