Change language

# Python | List the frequency of elements

|

Method # 1: Using ` Counter () + set () ` + comprehension list
A combination of the above functions can be used to accomplish a task. The Counter function does the grouping, the set function retrieves the individual items as dict keys and checks the list comprehension to appear in the list.

 ` # Python3 demo code ` ` # list of frequency elements ` ` # using Counter () + set () + comprehension list ` ` from ` ` collections ` ` import ` ` Counter ` ` `  ` # initializing list ` ` test_list ` ` = ` ` [[` ` 3 ` `, ` ` 5 ` `, ` ` 4 ` `], ` ` [` ` 6 ` `, ` ` 2 ` `, ` ` 4 ` `], ` ` ` ` [` ` 1 ` `, ` ` 3 ` `, ` ` 6 ` `]] ` ` `  ` # print the original list ` ` print ` ` (` `" The original list: "` ` + ` ` str ` ` (test_list)) `   ` # using Counter () + set () + comprehension list ` ` # list of element frequencies ` ` res ` ` = ` ` dict ` ` (Counter (i ` ` for ` ` sub ` ` in ` ` test_list ` ` for ` ` i ` ` in ` ` set ` ` (sub))) `   ` # print result ` ` print ` ` (` ` "The list frequency of elements is:" ` ` + ` ` str ` ` (res)) `

Output:

` The original list: [[3, 5, 4], [6, 2, 4], [1, 3, 6]] The list frequency of elements is: {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2} `

Method # 2: Using ` Counter () + itertools.chain.from_iterable () + map () + set ( ) `
The above 4 functions can also be combined to achieve this specific task. The set function retrieves the dictionary keys generated by Counter, the map function performs the task for all sublists, and the from_iterable function performs iterators, which is faster than comprehension of a list.

` `

` # Python3 demo code # list the frequency of items # using Counter () + itertools.chain.from_iterable () + map () + set () from collections import Counter from itertools import chain   # initializing list test_list = [[ 3 , 5 , 4 ], [ 6 , 2 , 4 ], [ 1 , 3 , 6 ]]   # print original list print ( "The original list:" + str (test_list))    # using Counter () + itertools.chain.from_iterable () + map () + set () # list of element frequencies res = dict (Counter (chain.from_iterable ( map ( set , test_list))))   # print result print ( " The list frequency of elements is: " + str (res)) `

` ` Output:

` The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]] The list frequency of elements is: {1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 2} `

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