Python – Elements frequency count in multiple lists
Last Updated :
27 Feb, 2023
Sometimes while working with Python lists we can have a problem in which we need to extract the frequency of elements in list. But this can be added work if we have more than 1 list we work on. Let’s discuss certain ways in which this task can be performed.
Method #1: Using dictionary comprehension + set() + count() This is one of the way in which this task can be performed. In this, we perform the task of counting using count() and set() and the extension of logic is done using dictionary comprehension.
Python3
test_list1 = [ 1 , 3 , 2 , 4 , 5 , 1 , 2 ]
test_list2 = [ 4 , 1 , 4 , 3 , 4 , 2 , 4 ]
test_list3 = [ 1 , 4 , 5 , 3 , 4 , 5 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
res = {idx : [test_list1.count(idx), test_list2.count(idx), test_list3.count(idx)]
for idx in set (test_list1 + test_list2 + test_list3)}
print ( "The frequency of each element is : " + str (res))
|
Output
The original list 1 is : [1, 3, 2, 4, 5, 1, 2]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The original list 3 is : [1, 4, 5, 3, 4, 5, 4]
The frequency of each element is : {1: [2, 1, 1], 2: [2, 1, 0], 3: [1, 1, 1], 4: [1, 4, 3], 5: [1, 0, 2]}
Time Complexity: O(n^2) and the space complexity is O(n) where n is the total number of elements in all the lists.
Auxiliary Space: O(n), where n is the number of elements in the input lists. The res dictionary and the set created for storing the unique elements of the input lists use O(n) space.
Method #2: Using Counter() + map() + dictionary comprehension The combination of above functionalities can be used to solve this problem. In this, the task of finding the frequency is done using Counter() and map().
Python3
from collections import Counter
test_list1 = [ 1 , 3 , 2 , 4 , 5 , 1 , 2 ]
test_list2 = [ 4 , 1 , 4 , 3 , 4 , 2 , 4 ]
test_list3 = [ 1 , 4 , 5 , 3 , 4 , 5 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
freq = list ( map (Counter, (test_list1, test_list2, test_list3)))
res = {ele: [cnt[ele] for cnt in freq] for ele in {ele for cnt in freq for ele in cnt}}
print ( "The frequency of each element is : " + str (res))
|
Output
The original list 1 is : [1, 3, 2, 4, 5, 1, 2]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The original list 3 is : [1, 4, 5, 3, 4, 5, 4]
The frequency of each element is : {1: [2, 1, 1], 2: [2, 1, 0], 3: [1, 1, 1], 4: [1, 4, 3], 5: [1, 0, 2]}
Time complexity: O(N*M), where N is the number of elements in each list and M is the number of lists.
Space complexity: O(N), where N is the number of unique elements across all the lists.
Method #3: Using extend(),list(),set() and count() methods
Python3
test_list1 = [ 1 , 3 , 2 , 4 , 5 , 1 , 2 ]
test_list2 = [ 4 , 1 , 4 , 3 , 4 , 2 , 4 ]
test_list3 = [ 1 , 4 , 5 , 3 , 4 , 5 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
x = []
res = dict ()
x.extend(test_list1)
x.extend(test_list2)
x.extend(test_list3)
x = list ( set (x))
for i in x:
a = []
a.append(test_list1.count(i))
a.append(test_list2.count(i))
a.append(test_list3.count(i))
res[i] = a
print ( "The frequency of each element is : " + str (res))
|
Output
The original list 1 is : [1, 3, 2, 4, 5, 1, 2]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The original list 3 is : [1, 4, 5, 3, 4, 5, 4]
The frequency of each element is : {1: [2, 1, 1], 2: [2, 1, 0], 3: [1, 1, 1], 4: [1, 4, 3], 5: [1, 0, 2]}
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4: Using dictionary comprehension + set() + operator.countOf() This is one of the way in which this task can be performed. In this, we perform the task of counting using operator.countOf() and set() and the extension of logic is done using dictionary comprehension.
Python3
import operator as op
test_list1 = [ 1 , 3 , 2 , 4 , 5 , 1 , 2 ]
test_list2 = [ 4 , 1 , 4 , 3 , 4 , 2 , 4 ]
test_list3 = [ 1 , 4 , 5 , 3 , 4 , 5 , 4 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
print ( "The original list 3 is : " + str (test_list3))
res = {idx : [op.countOf(test_list1,idx), op.countOf(test_list2,idx), op.countOf(test_list3,idx)]
for idx in set (test_list1 + test_list2 + test_list3)}
print ( "The frequency of each element is : " + str (res))
|
Output
The original list 1 is : [1, 3, 2, 4, 5, 1, 2]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The original list 3 is : [1, 4, 5, 3, 4, 5, 4]
The frequency of each element is : {1: [2, 1, 1], 2: [2, 1, 0], 3: [1, 1, 1], 4: [1, 4, 3], 5: [1, 0, 2]}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5: Using defaultdict
Use the defaultdict from the collections module to count the frequency of each element in the lists.
Python3
from collections import defaultdict
test_list1 = [ 1 , 3 , 2 , 4 , 5 , 1 , 2 ]
test_list2 = [ 4 , 1 , 4 , 3 , 4 , 2 , 4 ]
test_list3 = [ 1 , 4 , 5 , 3 , 4 , 5 , 4 ]
freq = defaultdict( lambda : [ 0 , 0 , 0 ])
for i in range ( len (test_list1)):
freq[test_list1[i]][ 0 ] + = 1
freq[test_list2[i]][ 1 ] + = 1
freq[test_list3[i]][ 2 ] + = 1
print ( dict (freq))
|
Output
{1: [2, 1, 1], 4: [1, 4, 3], 3: [1, 1, 1], 2: [2, 1, 0], 5: [1, 0, 2]}
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python - Matrix elements Frequencies Counter
Sometimes, while working with python Matrix, we can have a problem in which we need to find frequencies of all elements in Matrix. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using Counter() + sum() + map() The
5 min read
Python - List Frequency of Elements
We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}. Using collections.Countercollections.Counter class provides a dictionary-like structure that
2 min read
Python - Step Frequency of elements in List
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python | Frequency grouping of list elements
Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this
6 min read
Python | Find most frequent element in a list
Given a list, find the most frequent element in it. If multiple elements appear a maximum number of times, print any one of them using Python. Example Make a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus, we
2 min read
Frequency of Elements from Other List - Python
We are given a list of elements and another list containing specific values and our task is to count the occurrences of these specific values in the first list "a" and return their frequencies. For example: a = [1, 2, 2, 3, 4, 2, 5, 3, 1], b = [1, 2, 3]. Here b contains the elements whose frequency
3 min read
Python - Restrict Elements Frequency in List
Given a List, and elements frequency list, restrict frequency of elements in list from frequency list. Input : test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6], restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 1} Output : [1, 4, 5, 4, 4, 6] Explanation : Limit of 1 is 1, any occurrence more than that is removed.
5 min read
Python - Elements frequency in Tuple Matrix
Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved. Inp
5 min read
Python | Sort list elements by frequency
Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
3 min read
Python - Fractional Frequency of elements in List
Given a List, get fractional frequency of each element at each position. Input : test_list = [4, 5, 4, 6, 7, 5, 4, 5, 4] Output : ['1/4', '1/3', '2/4', '1/1', '1/1', '2/3', '3/4', '3/3', '4/4'] Explanation : 4 occurs 1/4th of total occurrences till 1st index, and so on.Input : test_list = [4, 5, 4,
5 min read