Counting the frequencies in a list using dictionary in Python
Given an unsorted list of some elements(may or may not be integers), Find the frequency of each distinct element in the list using a 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:
# Python program to count the frequency of # elements in a list using a dictionary 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) |
1 : 5 2 : 4 3 : 3 4 : 3 5 : 2
Time Complexity:O(N), where N is the length of the list.
Alternative way:An alternative approach can be to use the list.count() method. This makes the program much more compact without affecting the run time.Below is the Python code for this:
# Python program to count the frequency of # elements in a list using a dictionary 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) |
1 : 5 2 : 4 3 : 3 4 : 3 5 : 2
Time Complexity:O(N), where N is the length of the list.
Related Article :
Count frequencies of all elements in array in Python using collections module
Recommended Posts:
- Python dictionary, set and counter to check if frequencies can become same
- Counting frequencies of array elements
- Python | Counting sign change in list containing Positive and Negative Integers
- Python | Convert a list to dictionary
- Python | Clearing list as dictionary value
- Python | List value merge in dictionary
- Python | Updating value list in dictionary
- Python | Get dictionary keys as a list
- Python | Find dictionary matching value in list
- Print anagrams together in Python using List and Dictionary
- Python | Get Unique values from list of dictionary
- Python | Sort the list alphabetically in a dictionary
- Python | Convert dictionary to list of tuples
- Python | Converting list string to dictionary
- Python | Summation of dictionary list values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



