Open In App

Python | Find most common element in each column in a 2D list

Last Updated : 17 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a 2D list, write a Python program to find the most common element in each column of the 2D list. Examples:

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. 

Python3




# Python3 program to find most common
# element in each column in a 2D list
from collections import Counter
 
def mostCommon(lst):
         
    return [Counter(col).most_common(1)[0][0] for col in zip(*lst)]
         
# Driver code
lst = [[1, 1, 3],
       [2, 3, 3],
       [3, 2, 2],
       [2, 1, 3]]
print(mostCommon(lst))


Output:

[2, 1, 3]

Time Complexity: O(n*n), where n is the number of elements in the list 
Auxiliary Space: O(n), where n is the number of elements in the list 

  Method #2 : Using mode() from statistics module 

Python3




# Python3 program to find most common
# element in each column in a 2D list
from scipy.stats import mode
import numpy as np
 
def mostCommon(lst):
     
    val, count = mode(lst, axis = 0)
    return val.ravel().tolist()
         
# Driver code
lst = [[1, 1, 3],
       [2, 3, 3],
       [3, 2, 2],
       [2, 1, 3]]
print(mostCommon(lst))


Output:

[2, 1, 3]

Method #3 : Using recursion

This code defines a function most_common that takes a list of lists (lst) and an optional argument col (which is 0 by default). The function returns a list containing the most common element in each column of the input list.

The function first checks if col is equal to the length of the first sublist in lst. If it is, the function returns an empty list. This check is included so that the function knows when to stop iterating through the columns of lst.

Next, the function initializes an empty dictionary called count. This dictionary will be used to count the frequency of each element in the current column of lst.

The function then iterates through each row in lst and increments the count for the element in the col-th position of that row in the count dictionary. If the element is not already in the dictionary, it is added with a count of 1.

After all the rows have been processed, the function returns a new list with the element that appears most frequently in the col-th position of lst (which is found using the max function and the count.get key function) as the first element, and the result of calling most_common on lst with col incremented by 1 as the rest of the elements.

Finally, the code calls the most_common function on the list lst and prints the result.

Python




def most_common(lst, col=0):
    # Base case: if we have processed all the columns, return an empty list
    if col == len(lst[0]):
        return []
     
    # Initialize a dictionary to store the count of each element in the current column
    count = {}
    for row in lst:
        if row[col] in count:
            count[row[col]] += 1
        else:
            count[row[col]] = 1
     
    # Find the element with the highest count in the current column
    # and add it to the result list
    result = [max(count, key=count.get)]+most_common(lst, col+1)
    # Recursively find the most common element in the remaining columns
    return result
 
lst = [[1, 1, 3], [2, 3, 3], [3, 2, 2], [2, 1, 3]]
print(most_common(lst))
#This code is contributed by Edula Vinay Kumar Reddy


Output

[2, 1, 3]

Time complexity: O(mn), where m is the number of rows and n is the number of columns in the list.
Auxiliary Space: O(n), where n is the number of columns in the list.

Method 4: Using nested loops and dictionaries.

Algorithm:

  1. Initialize an empty list res to store the most common element in each column.
  2. Iterate over each column j in the 2D list lst.
  3. Initialize an empty dictionary count_dict to count the occurrence of each unique element in the column.
  4. Iterate over each row i in the column j of the 2D list lst.
  5. If the element lst[i][j] is already in the count_dict, increment its count by 1. Otherwise, add it to the dictionary with a count of 1.
  6. Find the element with the maximum count in the count_dict using the max() function and the key argument to specify the sorting criteria.
  7. Append the most common element to the res list.
  8. Return the res list.

Python3




lst = [[1, 1, 3],
    [2, 3, 3],
    [3, 2, 2],
    [2, 1, 3]]
 
res = []
for j in range(len(lst[0])):
    count_dict = {}
    for i in range(len(lst)):
        if lst[i][j] in count_dict:
            count_dict[lst[i][j]] += 1
        else:
            count_dict[lst[i][j]] = 1
    res.append(max(count_dict, key=count_dict.get))
print(res)


Output

[2, 1, 3]

Let n be the number of rows and m be the number of columns in the input 2D list lst.

Time Complexity: The outer loop iterates m times, and the inner loop iterates n times, so the overall time complexity is O(n * m).
Space Complexity: The space used by the res list is O(m), and the space used by the count_dict dictionary is at most O(n), so the overall space complexity is O(max(n, m)).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads