w3resource

Python: Find common element(s) in a given nested lists

Python List: Exercise - 122 with Solution

Write a Python program to find common element(s) in a given nested lists.

Sample Solution:

Python Code:

def common_in_nested_lists(nested_list):
    result = list(set.intersection(*map(set, nested_list)))
    return result
nested_list = [[12, 18, 23, 25, 45], [7, 12, 18, 24, 28], [1, 5, 8, 12, 15, 16, 18]]
print("\nOriginal lists:")
print(nested_list)
print("\nCommon element(s) in nested lists:")
print(common_in_nested_lists(nested_list))

Sample Output:

Original lists:
[[12, 18, 23, 25, 45], [7, 12, 18, 24, 28], [1, 5, 8, 12, 15, 16, 18]]

Common element(s) in nested lists:
[18, 12]

Flowchart:

Flowchart: Find common element(s) in a given nested lists.

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 create a list taking alternate elements from a given list.
Next: Write a Python program to reverse strings in a given list of string values.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Big Number Readability:

you can separate zeros with underscore (_)

print(2_000_000)
print(2_000_000 +1)

Output:

2000000
2000001