Open In App

Python | List frequency of elements

Last Updated : 25 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.
 

Python3




# Python3 code to demonstrate
# list frequency of elements
# using Counter() + set() + list comprehension
from collections import Counter
 
# initializing list
test_list = [[3, 5, 4],
             [6, 2, 4],
             [1, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using Counter() + set() + list comprehension
# list frequency of elements
res = dict(Counter(i for sub in test_list for i in set(sub)))
 
# printing 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}

 

Time Complexity: O(n^2), where n is the total number of tuples in the input “test_list”.

Auxiliary Space: O(n)

 
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 Counter
from itertools import chain
 
# initializing list
test_list = [[3, 5, 4],
             [6, 2, 4],
             [1, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using Counter() + itertools.chain.from_iterable() + map() + set()
# list frequency of elements
res = dict(Counter(chain.from_iterable(map(set, test_list))))
 
# printing 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 #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
 
# Original list
print(f"The original list : {test_list}" )
 
# printing result
print(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 pd
 
test_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']
 
# Original list
print(f"The original list : {test_list}" )
 
# printing result
print(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

Method#5Using defaultdict and loop:

Step-by-step algorithm:

  1. Import defaultdict from the collection’s module.
  2. Initialize the input list test_list
  3. Create a defaultdict object res with the default value of 0.
  4. Loop over each sublist in test_list.
  5. Loop over each element in the sublist.
  6. Increment the value of the res dictionary at the key corresponding to the current element by 1.
  7. Convert the res dictionary to a regular dictionary and print the result.

Python3




from collections import defaultdict
 
# initializing list
test_list = [[3, 5, 4],
            [6, 2, 4],
            [1, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using defaultdict and loop
# list frequency of elements
res = defaultdict(int)
for sublist in test_list:
    for elem in sublist:
        res[elem] += 1
 
# printing result
print("The list frequency of elements is : " + str(dict(res)))


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}

Time complexity: O(n^2), where n is the number of elements in test_list. This is because there are nested loops over the input list.

Auxiliary space: O(n), where n is the number of elements in test_list. This is because a dictionary is used to store the frequency of each element.

Method #6 : Using list(),set(),extend(),count() methods

Approach

  1. Create an empty dictionary res
  2. Convert nested list test_list to list x using extend()+for loop
  3. Create a list y with unique values of x using list(),set() methods
  4. Initiate a for loop to assign values of y as keys and count of these values in x as values to dictionary res
  5. Display res

Python3




# Python3 code to demonstrate
# list frequency of elements
 
# initializing list
test_list = [[3, 5, 4],
            [6, 2, 4],
            [1, 3, 6]]
 
# printing original list
print("The original list : " + str(test_list))
 
 
# list frequency of elements
res = dict()
x=[]
for i in test_list:
    x.extend(i)
y=list(set(x))
for i in y:
    res[i]=x.count(i)
     
# printing 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}

Time Complexity : O(N) N – length of extended list 

Auxiliary Space : O(N) N – length of dictionary res

Method #7 : Using reduce():

Algorithm :

  1. Import the reduce function from the functools module.
  2. Initialize a nested list, test_list, with integer elements.
     
  3. Print the original nested list.
     
  4. Use the reduce() function with a lambda function to flatten the nested list into a flat list.
  5. The lambda function takes two arguments x and y which represent the accumulator and the current element respectively.
  6. The lambda function concatenates the two lists x and y using the + operator.
  7. Use the reduce() function with another lambda function to compute the frequency of each element in the flat list.
  8. The lambda function takes two arguments x and y which represent the accumulator and the current element respectively.
     
  9. The lambda function returns a dictionary x with an updated count of the current element y.
  10. If the current element y is not in the dictionary, the get() method returns 0.
  11. The ** syntax is used to unpack the dictionary x and update it with a new key-value pair {y: x.get(y, 0) + 1}.
  12. Print the resulting dictionary containing the frequency of elements.
     

Python3




from functools import reduce
 
# initializing list
test_list = [[3, 5, 4],
            [6, 2, 4],
            [1, 3, 6]]
# printing original list
print("The original list : " + str(test_list))
  
# flatten the nested list using reduce and extend
flat_list = reduce(lambda x, y: x + y, test_list)
 
# compute frequency of elements using reduce and dict
freq_dict = reduce(lambda x, y: {**x, y: x.get(y, 0) + 1}, flat_list, {})
 
# printing result
print("The list frequency of elements is : " + str(freq_dict))
#This code is contrinuted by Pushpa.


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}

Time Complexity: O(N*M) where N is the number of sub-lists in the input list and M is the average length of each sub-list. The first reduce() function iterates over each sub-list in the input list and the extend() method inside the lambda function iterates over each element in the sub-list. The second reduce() function also iterates over each element in the flattened list.

Space Complexity:  O(NM) where N is the number of sub-lists in the input list and M is the maximum length of each sub-list. The flattened list created by the first reduce() function can be as large as NM. The dictionary created by the second reduce() function will also contain N*M key-value pairs in the worst case.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads