Python | Find most common element in a 2D list
Given a 2D list (may or may not be of same length), write a Python program to find the most common element in the given 2D list.
Examples:
Input : [[10, 20, 30], [20, 50, 10], [30, 50, 10]] Output : 10 Input : [['geeks', 'wins'], ['techie', 'wins']] Output : wins
Approach #1 : Using max() function
First Pythonic approach is to use max() method of Python. We first flatten the 2D list and then simply apply max() method to find out the maximum occurring element among all the elements.
# Python3 program to find most # common element in a 2D list def mostCommon(lst): flatList = [el for sublist in lst for el in sublist] return max(flatList, key = flatList.count) # Driver code lst = [[10, 20, 30], [20, 50, 10], [30, 50, 10]] print(mostCommon(lst)) |
10
There is another method to flatten the list i.e chain.from_iterable() which gives rise to an alternative approach.
# Python3 program to find most # common element in a 2D list from itertools import chain def mostCommon(lst): flatList = list(chain.from_iterable(lst)) return max(flatList, key=flatList.count) # Driver code lst = [[10, 20, 30], [20, 50, 10], [30, 50, 10]] print(mostCommon(lst)) |
10
Approach #2 : Using most_common() from collections module
most_common() is used to produce a sequence of the n most frequently encountered input values. Therefore, we simply flatten the list and find the most common element using above mentioned method.
# Python3 program to find most # common element in a 2D list from itertools import chain from collections import Counter def mostCommon(lst): flatList = chain.from_iterable(lst) return Counter(flatList).most_common(1)[0][0] # Driver code lst = [[10, 20, 30], [20, 50, 10], [30, 50, 10]] print(mostCommon(lst)) |
10
Recommended Posts:
- Python | Find most common element in each column in a 2D list
- Python | Find common elements in list of lists
- Python | Find most frequent element in a list
- Python | Merge List with common elements in a List of Lists
- Python | Check if two lists have any element in common
- Python | Check if two lists have at-least one element common
- Python | Find the tuples containing the given element from a list of tuples
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- intersection_update() in Python to find common elements in n arrays
- Python program to find common elements in three lists using sets
- Python | How to get the last element of list
- Python | Every Kth element in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



