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:

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.
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()
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
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