w3resource

Python: Takes two lists and returns True if they have at least one common member

Python List: Exercise-11 with Solution

Write a Python function that takes two lists and returns True if they have at least one common member.

Pictorial Presentation:

Python: Takes two lists and returns True if they have at least one common member

Sample Solution-1:

Python Code:

def common_data(list1, list2):
     result = False
     for x in list1:
         for y in list2:
             if x == y:
                 result = True
                 return result
print(common_data([1,2,3,4,5], [5,6,7,8,9]))
print(common_data([1,2,3,4,5], [6,7,8,9]))

Sample Output:

True                                                                                                          
None 

Explanation:

The above code defines a function called "common_data" that takes two lists as arguments: list1 and list2. The function initializes a Boolean variable result to False.

The function then loops through each element in list1 and list2 using nested for loops. If the current element in list1 is equal to the current element in list2, the function sets result to True and immediately returns the result using the return statement. This means that the function will stop execution as soon as it finds a common element between the two lists.

print(common_data([1,2,3,4,5], [5,6,7,8,9]))
print(common_data([1,2,3,4,5], [6,7,8,9]))

The above lines call the "common_data" function twice, with different sets of input lists.

The first call passes [1, 2, 3, 4, 5] and [5, 6, 7, 8, 9] as arguments. Since both lists have the element 5 in common, the function sets result to True and immediately returns it. Therefore, the first call to common_data will print True.

The second call passes [1, 2, 3, 4, 5] and [6, 7, 8, 9] as arguments. Since there are no elements that appear in both lists, the function will not set result to True and will not return a value. Therefore, the second call to common_data will print None.

Flowchart:

Flowchart: Takes two lists and returns True if they have at least one common member

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Sample Solution-2:

  • Check if any value in lsts is contained in nums using a for loop.
  • Return True if any one value is found, False otherwise.

Python Code:

def test_includes_any(nums, lsts):
    for x in lsts:
        if x in nums:
            return True
    return False    
print(test_includes_any([10, 20, 30, 40, 50, 60], [22, 42]))
print(test_includes_any([10, 20, 30, 40, 50, 60], [20, 80]))

Sample Output:

False
True

Explanation:

The above code defines a function called test_includes_any that takes two arguments: a list of integers nums, and a list of lists of integers lsts.

The function then loops through each list x in lsts, and for each list it checks if any of the integers in the list are also in nums. If it finds a match, it returns True.

If it loops through all the lists in lsts without finding a match, it returns False.

print(test_includes_any([10, 20, 30, 40, 50, 60], [22, 42]))
print(test_includes_any([10, 20, 30, 40, 50, 60], [20, 80]))

In the first call, nums is [10, 20, 30, 40, 50, 60] and lsts is [[22, 42]]. The function checks if any of the integers in [22, 42] are in [10, 20, 30, 40, 50, 60], and since 22 or 42 not present in second list, it returns False.

In the second call, nums is [10, 20, 30, 40, 50, 60] and lsts is [[20, 80]]. The function checks if any of the integers in [20, 80] are in [10, 20, 30, 40, 50, 60], and since 20 is in both lists, it returns True.

Flowchart:

Flowchart: Takes two lists and returns True if they have at least one common member

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the list of words that are longer than n from a given list of words.
Next: Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Given a predicate function, fn, and a prop string, this curried function will then take an object to inspect by calling the property and passing it to the predicate:

Example:

def tips_check_prop(fn, prop):
  return lambda obj: fn(obj[prop])
check_age = tips_check_prop(lambda x: x >= 25, 'age')
user = {'name': 'Owen', 'age': 25}

print(check_age(user))

Output:

True 

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook