
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find common elements in list of lists in Python
It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.
With map and intersection
Intersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the set method to all this converted lists.
Example
listA = [['Mon', 3, 'Tue', 7,'Wed',4],['Thu', 5,'Fri',11,'Tue', 7],['Wed', 9, 'Tue', 7,'Wed',6]] # Given list print("Given list of lists : \n",listA) # Applying intersection res = list(set.intersection(*map(set, listA))) # Result print("The common elements among inners lists : ",res)
Output
Running the above code gives us the following result −
Given list of lists : [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inners lists : ['Tue', 7]
With reduce and lambda
We can also apply the reduce function in python. This function is used to apply a given function passed onto it as argument to all of the list elements mentioned in the sequence passed along. The lambda function finds out the common elements by iterating through each nested list after set is applied to them .
Example
from functools import reduce listA = [['Mon', 3, 'Tue', 7,'Wed',4],['Thu', 5,'Fri',11,'Tue', 7],['Wed', 9, 'Tue', 7,'Wed',6]] # Given list print("Given list of lists : \n",listA) # Applying reduce res = list(reduce(lambda i, j: i & j, (set(n) for n in listA))) # Result print("The common elements among inners lists : ",res)
Output
Running the above code gives us the following result −
Given list of lists : [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inners lists : ['Tue', 7]
- Related Articles
- Python program to find common elements in three lists using sets
- Find common elements in three linked lists in C++
- Python - Convert column to separate elements in list of lists
- Get positive elements from given list of lists in Python
- Program to find highest common factor of a list of elements in Python
- Python program to print all the common elements of two lists.
- Find minimum of each index in list of lists in Python
- Convert list into list of lists in Python
- Program to interleave list elements from two linked lists in Python
- Minimum Index Sum for Common Elements of Two Lists in C++
- Custom Multiplication in list of lists in Python
- Python Program that print elements common at specified index of list elements
- Find sum of elements in list in Python program
- How to join list of lists in python?
- Find missing elements in List in Python
