Exercise 30 

Write a function in Python that allows to compare two lists and to tell us if these two lists have a common value or not.

Solution

# define a function that compare two lists l1 and l2
def commonElements(l1,l2):
counter = 0
# try if there exists a common element and then increment the counter
for x in l1:
for y in l2:
if(x == y):
counter = counter + 1
if counter >=1:
return True
else:
return False

# Example
l1 = [3, 9, 4, 7]
l2 = [1, 14, 3, 7, 5]
print(commonElements(l1,l2))
Younes Derfoufi

Leave a Reply