Open In App
Related Articles

Python – Elements frequency count in multiple lists

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

# Python3 code to demonstrate
# Elements frequency count in multiple lists
# using set() + count() + dictionary comprehension
 
# Initializing lists
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]
 
# printing original lists
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))
 
# Elements frequency count in multiple lists
# using set() + count() + dictionary comprehension
res = {idx : [test_list1.count(idx), test_list2.count(idx), test_list3.count(idx)]
    for idx in set(test_list1 + test_list2 + test_list3)}
             
# printing result
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

# Python3 code to demonstrate
# Elements frequency count in multiple lists
# using map() + Counter() + dictionary comprehension
from collections import Counter
 
# Initializing lists
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]
 
# printing original lists
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))
 
# Elements frequency count in multiple lists
# using map() + Counter() + dictionary comprehension
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}}
             
# printing result
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

# Python3 code to demonstrate
# Elements frequency count in multiple lists
 
# Initializing lists
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]
 
# printing original lists
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))
 
# Elements frequency count in multiple lists
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
 
# printing result
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

# Python3 code to demonstrate
# Elements frequency count in multiple lists
# using set() + operator.countOf() + dictionary comprehension
import operator as op
# Initializing lists
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]
 
# printing original lists
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))
 
# Elements frequency count in multiple lists
# using set() + operator.countOf() + dictionary comprehension
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)}
             
# printing result
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)



Last Updated : 27 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads