# Python | Find the most common item in each column in a 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 the collections

most_common () module returns a list from n the most common elements and their number from the most common to the smallest. This way we can easily find the most common elements in each column using a list comprehension.

 ` # Python3 program to find the most common ` ` # item in every column in the 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)) `

Exit:

` [2, 1, 3] `

Method # 3: Using mode () from statistics module

 ` # Python3 program for finding common ones ` ` # item in every column in 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)) `

Exit:

` [2, 1, 3] `

## Shop Learn programming in R: courses

\$ Best Python online courses for 2022

\$ Best laptop for Fortnite

\$ Best laptop for Excel

\$ Best laptop for Solidworks

\$ Best laptop for Roblox

\$ Best computer for crypto mining

\$ Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method