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 most frequent element in a list
- Python | Merge List with common elements in a List of Lists
- Python | Check if two lists have at-least one element common
- Python | Check if two lists have any element in 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 | Slicing list from Kth element to last element
- intersection_update() in Python to find common elements in n arrays
- Python | Three element sum in list
- Python | How to get the last element of list
- Python program to find common elements in three lists using sets
- Python | Insert after every Nth element in a list
- Python | Last occurrence of some element in a 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 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.