Given a 2D list, write a Python program to find the most common element in each column of the 2D list.
Input : [[1, 1, 3], [2, 3, 3], [3, 2, 2], [2, 1, 3]] Output : [2, 1, 3] Input : [['y', 'y'], ['y', 'x'], ['x', 'x']] Output : ['y', 'x']
Method #1 : Using most_common() from collections module
most_common() return a list of the n most common elements and their counts from the most common to the least. Thus, we can easily find the most common elements in each column using list comprehension.
[2, 1, 3]
Method #3 : Using mode() from statistics module
[2, 1, 3]
- Python | Find most common element in a 2D list
- Python - Add K to Minimum element in Column Tuple List
- Python | Find common elements in list of lists
- Python | Find most frequent element in a list
- Python - Find first element by second in tuple 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 | Tuple Column element frequency
- Python | Column Mean in tuple list
- Python - Add Custom Column to Tuple list
- Python | Search in Nth column in list of tuples
- Python - Column Minimum in Tuple list
- Python | Column Product in List of lists
- Python | Summation of Kth Column of Tuple List
- Python | Product of kth column in List of Lists
- Python - Get maximum of Nth column from tuple list
- Python | Column Average in Record List
- Python | Column deletion from list of lists
- Python | Maximize Column in Records 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.