Python | List frequency of elements
Sometimes we have the utility in which we require to find the frequency of elements in the list and the solution to this problem has been discussed many times. But sometimes we come across the task in which we require to find the number of lists that particular elements occur. Let’s discuss certain shorthands in which this can be done.
Method #1 : Using Counter() + set() + list comprehension
The combination of the above functions can be used to perform the task. The Counter function does the grouping, set function extracts the distinct elements as keys of dict and list comprehension check for its list occurrences.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python3
# Python3 code to demonstrate# list frequency of elements# using Counter() + set() + list comprehensionfrom collections import Counter# initializing listtest_list = [[3, 5, 4], [6, 2, 4], [1, 3, 6]]# printing original listprint("The original list : " + str(test_list))# using Counter() + set() + list comprehension# list frequency of elementsres = dict(Counter(i for sub in test_list for i in set(sub)))# printing resultprint("The list frequency of elements is : " + str(res)) |
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 functionalities can also be combined to achieve this particular task. The set function extracts the dictionary keys formed by the Counter, map function performs the task for all sublists and from_iterable function performs using iterators which is faster than list comprehension.
Python3
# Python3 code to demonstrate# list frequency of elements# using Counter() + itertools.chain.from_iterable() + map() + set()from collections import Counterfrom itertools import chain# initializing listtest_list = [[3, 5, 4], [6, 2, 4], [1, 3, 6]]# printing original listprint("The original list : " + str(test_list))# using Counter() + itertools.chain.from_iterable() + map() + set()# list frequency of elementsres = dict(Counter(chain.from_iterable(map(set, test_list))))# printing resultprint("The list frequency of elements is : " + str(res)) |
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 #3: Using python dictionary + get() method
Python dictionary provides a get method which returns the value corresponding to the key and if the key does not exist in the dictionary then it provides functionality to create the key and assign it a default value. We will use this functionality of a dictionary.
Python3
d = {}test_list = [[3, 5, 4], [6, 2, 4], [1, 3, 6]]for x in test_list: for i in x: d[i] = d.get(i,0) + 1# Orignal listprint(f"The original list : {test_list}" )# printing resultprint(f"The list frequency of elements is : {d}" ) |
Output:
The original list : [[3, 5, 4], [6, 2, 4], [1, 3, 6]]
The list frequency of elements is : {3: 2, 5: 1, 4: 2, 6: 2, 2: 1, 1: 1}Method #4: Using Pandas
In this method we will use a python module named pandas(You can know more about pandas in this article) to find the frequency of the given data, here below is the code for it.
Python3
import pandas as pdtest_list = [3,5,4,3,3,4,5,2]df1 = pd.Series(test_list).value_counts().sort_index().reset_index().reset_index(drop=True)df1.columns = ['Element', 'Frequency']# Orignal listprint(f"The original list : {test_list}" )# printing resultprint(f"The list frequency of elements is :\n {df1.to_string(index=False)}" ) |
Output:
The original list : [3, 5, 4, 3, 3, 4, 5, 2] The list frequency of elements is : Element Frequency 2 1 3 3 4 2 5 2


