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.
Input : [[10, 20, 30], [20, 50, 10], [30, 50, 10]] Output : 10 Input : [['geeks', 'wins'], ['techie', 'wins']] Output : wins
Approach #1 : Using
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.
There is another method to flatten the list i.e
chain.from_iterable() which gives rise to an alternative approach.
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.
- Python | Find most common element in each column in a 2D list
- Python | Find common elements in list of lists
- Python - Find first element by second in tuple List
- Python | Find most frequent element in a list
- Python | Find frequency of largest element in list
- Python - Find the index of Minimum element in 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 - Common items Dictionary Value List
- Python | Find the tuples containing the given element from a list of tuples
- Python | Subtract two list elements if element in first list is greater
- 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 | Insert Nth element to Kth element in other list
- 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 | Find common elements in three sorted arrays by dictionary intersection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.