Change language

# Python | Sort list items by frequency

|

Method # 1: Using `collections.counter()`

` `

` # Python code for demo # sort the list by frequency number of items   from collections import Counter   ini_list = [ 1 , 2 , 3 , 4 , 4 , 5 , 5 , 5 , 5 , 7 ,  1 , 1 , 2 , 4 , 7 , 8 , 9 , 6 , 6 , 6 ]    # print the initial ini_lis t print ( "initial list" , str (ini_list))   # sort by element frequency byte result = [item for items, c in Counter (ini_list) .most_common () for item in [items] * c]   # print the final result pr int ( "final list" , str (result)) `

` ` Exit :

initial list [1, 2, 3, 4, 4, 5, 5, 5, 5, 5, 7, 1, 1, 2 , 4, 7, 8, 9, 6, 6, 6]
final list [5, 5, 5, 5, 1, 1, 1, 4, 4, 4, 6, 6, 6, 2, 2, 7, 7, 3, 8, 9]

Method # 2: Using iterations

 ` # Python code for demo ` ` # sort the list by frequency ` ` number of elements `   ` from ` ` collections ` ` import ` ` Counter ` ` from ` itertools ` import ` ` repeat, chain `   ` ini_list ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 4 ` `, ` ` 4 ` `, ` ` 5 ` `, ` ` 5 ` `, ` ` 5 ` `, ` ` 5 ` `, ` ` 7 ` `, ` ` 1 ` `, ` ` 1 ` `, ` ` 2 ` `, ` ` 4 ` `, ` ` 7 ` `, ` ` 8 ` `, ` ` 9 ` `, ` ` 6 ` `, ` ` 6 ` `, ` ` 6 ` ` ] `   ` # print the initial ini_list ` ` print ` ` (` ` "initial list" ` `, ` ` str ` ` (ini_list)) ` ` `  ` # sorting by element frequency byte ` ` result ` ` = ` ` list (chain.from_iterable (repeat (i, c) `` for i, c in Counter (ini_list) .most_common ()))   # print the final result print ( "final list" , str (result)) `

Exit:

initial list [1, 2, 3, 4, 4, 5, 5, 5, 5, 7, 1, 1, 2, 4, 7, 8, 9, 6, 6, 6]
final list [5, 5, 5, 5, 1, 1, 1, 4 , 4, 4, 6, 6, 6, 2, 2, 7, 7, 3, 8, 9]

Method # 3: Usage sorted

 # Python code for demo ` # sort the list by frequency ` ` amount elements `   ` from ` ` collections ` ` import ` ` Counter ` ` `  ` ini_list ` ` = ` ` [` ` 1 ` `, ` ` 1 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 2 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 3 ` `, ` ` 5 ` `, ` ` 5 ` `, ` ` 5 ` `, ` ` 4 ` `, ` ` 4 ` `, ` ` 4 ` `, ` ` 4 ` `, ` ` 4 ` `, ` ` 4 ` `] `   ` # print the initial ini_list ` ` print ` ` (` ` "initial list" ` `, ` ` str ` ` (ini_list)) ` ` `  ` # sort by element frequency byte ` ` result ` ` = ` ` sorted ` ` (ini_list, key ` ` = ` ` ini_list.count, ` ` ` ` reverse ` ` = ` ` True ` `) `   ` # print the final result ` ` print ` ` (` ` "final list" ` `, ` ` str ` ` (result)) `

Exit:

initial list [1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 4, 4, 4, 4, 4, 4]
final list [4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 5, 5, 5, 1, 1]

## 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

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries