Python-courses-python-exercises-python-for-while

Exercise14

Write a Python program as a function that takes two lists as parameters and returns True if the two lists have at least one common element and False if not.

Solution

def commonElement(L1 , L2):
result = False
for x in L1:
for y in L2:
if x == y :
result = True
return result
# testing the algorithm
L1 = ["Python" ,"Java" ,"C++" ,"C#" ,"PHP" ,"NodeJS"]
L2 = ["Python" ,"Dart" ,"PHP" ,"NodeJS" , "Django" ,"Laravel" ]
L3 = ["HTML" , "CSS" , "Javascript"]
print(commonElement(L1 , L2)) # the output is : True
print(commonElement(L1 , L3)) # the output is : False

Younes Derfoufi
my-courses.net

Leave a Reply