Given an unsorted list of some elements(which may or may not be integers), Find the frequency of each distinct element in the list using a Python dictionary.
Example:
Input: [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2] Output: 1 : 5 2 : 4 3 : 3 4 : 3 5 : 2 Explanation: Here 1 occurs 5 times, 2 occurs 4 times and so on...
The problem can be solved in many ways. A simple approach would be to iterate over the list and use each distinct element of the list as a key of the dictionary and store the corresponding count of that key as values. Below is the Python code for this approach:
Approach 1: Counting the frequencies in a list using a loop
In this method, we will use a Python loop to count the distinct element from the list and append it to the dictionary.
Python3
def CountFrequency(my_list): # Creating an empty dictionary freq = {} for item in my_list: if (item in freq): freq[item] + = 1 else : freq[item] = 1 for key, value in freq.items(): print ( "% d : % d" % (key, value)) # Driver function if __name__ = = "__main__" : my_list = [ 1 , 1 , 1 , 5 , 5 , 3 , 1 , 3 , 3 , 1 , 4 , 4 , 4 , 2 , 2 , 2 , 2 ] CountFrequency(my_list) |
Output:
1 : 5 2 : 4 3 : 3 4 : 3 5 : 2
Time Complexity: O(N), where N is the length of the list.
Approach 2: Count the frequencies in a list using list.count()
An alternative approach can be to use the list.count() method.
Python3
import operator def CountFrequency(my_list): # Creating an empty dictionary freq = {} for items in my_list: freq[items] = my_list.count(items) for key, value in freq.items(): print ( "% d : % d" % (key, value)) # Driver function if __name__ = = "__main__" : my_list = [ 1 , 1 , 1 , 5 , 5 , 3 , 1 , 3 , 3 , 1 , 4 , 4 , 4 , 2 , 2 , 2 , 2 ] CountFrequency(my_list) |
Output:
1 : 5 2 : 4 3 : 3 4 : 3 5 : 2
Time Complexity: O(N2), where N is the length of the list. The time complexity list.count() is O(N) alone, and when used inside the loop it will become O(N2).
Approach 3: Count the frequencies in a list using dict.get()
The dict.get() method, makes the program much shorter and makes understanding how the get method is useful instead of if…else.
Python3
def CountFrequency(my_list): # Creating an empty dictionary count = {} for i in [ 1 , 1 , 1 , 5 , 5 , 3 , 1 , 3 , 3 , 1 , 4 , 4 , 4 , 2 , 2 , 2 , 2 ]: count[i] = count.get(i, 0 ) + 1 return count # Driver function if __name__ = = "__main__" : my_list = [ 1 , 1 , 1 , 5 , 5 , 3 , 1 , 3 , 3 , 1 , 4 , 4 , 4 , 2 , 2 , 2 , 2 ] print (CountFrequency(my_list)) |
Output:
{1: 5, 5: 2, 3: 3, 4: 3, 2: 4}
Related Article: Count frequencies of all elements in array in Python using collections module
Count the frequencies in a list using operator.countOf() method
Approach 4:
- Created a dictionary with keys as unique list values and values as unique element count in list using for loop and operator.countOf() method.
- Displayed the keys and values of the dictionary.
Python3
import operator def CountFrequency(my_list): # Creating an empty dictionary freq = {} for items in my_list: freq[items] = operator.countOf(my_list, items) for key, value in freq.items(): print ( "% d : % d" % (key, value)) # Driver function if __name__ = = "__main__" : my_list = [ 1 , 1 , 1 , 5 , 5 , 3 , 1 , 3 , 3 , 1 , 4 , 4 , 4 , 2 , 2 , 2 , 2 ] CountFrequency(my_list) |
1 : 5 5 : 2 3 : 3 4 : 3 2 : 4
Time Complexity : O(N*N)
N -length of list
Auxiliary Space : O(1)