w3resource

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

Python List: Exercise - 122 with Solution

Write a Python program to find common elements in a nested list.

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 Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()

 





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