Write python code to count the frequencies in a list using dictionary

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...

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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




# 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)
Output: 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.

Python




# 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)
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 loop it will become O(N2).

Alternative way:An alternative approach can be to use the dict.get() method. This makes the program much more shorter and makes understand how get method is useful instead of if…else.

Python




# Python program to count the frequency of
# elements in a list using a dictionary
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




Article Tags :
Python
frequency-counting
Python dictionary-programs
python-dict
python-list
Practice Tags :
python-dict
python-list
Read Full Article

Counting the frequencies in a list using dictionary in Python

In this tutorial, you will learn to write a program in Python that will count the frequency of elements in a list using a dictionary. For a given list with multiple elements, we have to count the occurrence of each element in the list and display it as output. This can be done by creating a dictionary and using some built-in methods which will be discussed below.

Look at the input-output format to understand better.

Input: ['a', 'a', 'a', 'b','b']

Output: {'a': 3, 'b': 2}

To solve this problem we can follow these approaches-

  1. by iterating over the list and counting frequency
  2. using list.count() method
  3. using dict.get() method

Counting the frequencies in a list using dictionary in Python

PythonServer Side ProgrammingProgramming

In this article we develop a program to calculate the frequency of each element present in a list.

“counting the frequency of elements in a list using a dictionary in python” Code Answer’s


list count frequency python
python by Cheerful Cormorant on Jul 15 2020 Comment
1
Source: stackoverflow.com
freq count in python
python by Amused Albatross on Jul 21 2020 Comment
3
Add a Grepper Answer

Python answers related to “counting the frequency of elements in a list using a dictionary in python”

  • find number of unique keys in the dictionary
  • python find number of occurrences in list
  • python count number of unique elements in a list
  • convert list of values to dictionary based on count
  • python counting dictionary
  • get number of key in dictionary python
  • find frequency of numbers in list python
  • python count the frequency of words in a list
  • count how many times a value shows in python list
  • count different values in list python
  • how to get frequency of each elements in a python list
  • Python - Count the Number of Keys in a Python Dictionary
  • python count appearances in list
  • count number of occurrences of all elements in list python
  • count unique elements in list python
  • count number of each item in list python

Python queries related to “counting the frequency of elements in a list using a dictionary in python”

  • given a list iterate it and count the occurrence of each element and create a dictionary to show the count of each element
  • count frequency of elements in list python
  • maximum frequency python using dictionary
  • wap to count frequency of a given element in a list of numbers.
  • how to count frequency of elements in a list in python
  • python frequency count
  • how to count frequency in dictionary python
  • list to dictionary with occurances python
  • frequency python
  • freq count in python
  • frequency in list python
  • python frequency list
  • count function python
  • python dictionary frequency
  • python get frequency of items in list
  • frequency in a list python
  • count dict list python
  • how to count the frequency of a value in python
  • frequency of items in list python
  • how to count the frequency of a value list python
  • count() python
  • python list group by count
  • frequency of list in python
  • how to find frequency of a number in a list in python
  • python frequency of list
  • how to find frequency of number in list python
  • python get frequency of elements in list
  • python count word frequency
  • how to use count in python
  • count for making identical distribution in python pgm single list
  • how to know the frequency of a number in list in python
  • how to count frequency of list of lists python
  • counting the frequency of elements in a list
  • finding frequency python dictionay
  • python count frequency in array
  • python get frequency of list elements
  • python count function
  • count the frequency of all the list elements and store the frequency in a dictionary.
  • count list elements dictionary python
  • python count()
  • what is frequency of element in list python
  • frequency of element in list python using dictionary
  • python how to get the count in a for loop
  • key frequency python
  • count frequency in a conplex dictionary python
  • how to check frequency of elements in list in python
  • find freqency in python
  • how to find frequncy in python
  • python how to set the frequency of a value in a dict
  • all freq function in python
  • .count python
  • code for frequancy python
  • hash table in python to store frequency of array
  • python dictionary creation from list with value counts
  • count string frequency python
  • how to set list items frequency wise in python
  • find frequency of number in list python
  • python how to set the data frequency of a value in a dict
  • how to assort a list into a dictionary with the counts in python
  • how to create a counting with a dictionary in python
  • count loop python
  • count in python'
  • python count frequency map
  • get count of elements in list python using dictionary
  • .count() python
  • how to count multiple occurrences in a dictionary list
  • write code in python to calculate and display the frequency of each item in a list
  • list in python to dictionary with frequency
  • create dictionary counting list values python
  • how to get the frequency of a value in a list python
  • frequency of an elemnet in list python
  • how to get frq in python
  • python frequency dict
  • list of dictionary python count frequency
  • python count syntax
  • python creating a dictionary of counting integers
  • frequency count list
  • frequency count on ordered dictonary
  • no of frequency counter python
  • frequency list python count
  • python get frequency from list
  • frequency of the elements in a list
  • count character frequency python
  • frenquency count of python list
  • frequency of a value in a list python
  • how to count which element has the max frequency in py in lists
  • count frequency of list python
  • make frequency array in pythin
  • make a list for n number and frequency in python
  • element *frequency list python
  • frequency of elements in list in python
  • frequency of output value python
  • find frequency of elements in python ,lst
  • store a freq file into a dict python
  • python get frequnecy of values in list
  • count frequency of numbersin a list
  • python get number of same element in list without know them
  • how to count frequency of item in list python
  • count frequency of the values in a list
  • count frequency in list
  • calculate frequency in python in a list
  • count frequency range list python
  • count frequency of elements in many lists
  • python count list elements frequency
  • find frequency of number in adjacency list python
  • count frequency of element in adjacency list
  • count element frequency pythonb
  • count frequency in array library python
  • count frequency of one element in list python
  • count frequency of number in list python
  • count frequency of item in list python
  • frequency on list python
  • python count element frequency in list
  • list get frequency of item python
  • frequency count in list python
  • count frequency of each element in a list python
  • best way to count frequency in -python
  • how to identify frequency in a given list in python program
  • find frequency of a number in list python
  • python list get frequency
  • freq counter python
  • python count frequency in list
  • python count frequency of elements in list
  • frequency of element in list python
  • get frequency of elements in list python
  • find frequency of characters in a list python using dictionary
  • how to find frequency of elements in list in python
  • python put the frequency dictionary in list
  • python list count frequency
  • python list frequency count
  • python frequency of items in list
  • counting the frequency of elements in a list using a dictionary in python
  • frequency in python
  • use dictionary to count frequency python
  • count function in python
  • python count frequency of items in list
  • frequency count list python
  • do not print the frequency of numbers in python
  • python frequency count of list
  • python list frequency
  • python count frequency of numbers in a list
  • how to use count python
  • python count list element frequency
  • find frequency of numbers in list python
  • number frequency in python
  • how to count the frequency of elements in a list in python
  • count frequencies in list of dictionaries python
  • frequency of elements in a list python
  • dict of how many times character is in a list python
  • python frequency of number in list
  • frequency of numbers in list
  • how to count number frequency in python
  • list to dictionary python count
  • create a list iterate it and count the occurrence of each element and create a dictionary to show the count of each element
  • count frequency of an element in list python
  • frequency of element of list in python
  • python code to count frequency of a number in a list
  • count freq in python
  • how to count key frequency in python
  • find frequency of element in list python
  • python list frequency table
  • counting the frequencies in a list using dictionary in python
  • python count number frequency
  • python frequency using get()
  • count the frequency of all the list elements and store the frequency in a dictionary in python
  • python count frequency in dictionary
  • get frequency of elements in list pandas
  • count freuencies of elements in list
  • python hashtable frequency counter
  • key=freq.get
  • hash table in python for frequency
  • python count elements in list to dict
  • display frequencies of words in an array using dictionaries
  • freequnecy dictionary in python
  • create a dict of the number of times an element appears in a list python
  • convert array of count of numbers to dictionary in python
  • how to generate a dictionary from a list in python with the values being the frequency of the key
  • given a list [1, 2, 3, 1, 2, 3, 4, 5, 5, 8, 1]. write code in python to convert this list to a dictionary where the elements of the dictionary should be stored with their frequencies.
  • freq function python
  • count each element occurrence in python list without function using dictionary
  • frequency function in python
  • make dictionary of count of letters in words
  • how to count dictionary in list python
  • what is count() in python
  • python count value frequency in dict python
  • how to use .count in python
  • python .count
  • python count.
  • frequency table from dictionary python
  • how to use the count function in python
  • frequency in list
  • how to use a dictionary to count frequency?
  • python string freqeuncy
  • frequency hash map python
  • list do dict and frequency
  • freq in python
  • how to check frequency using list
  • frequency count on ordered dictonary python
  • number of occurrences of an element in a list in python dict
  • python frequency element liste
  • python get frequency
  • python group list by count
  • python frequency list count
  • find frequency in list
  • freq python
  • python frequency of words in list
  • python count frequency of words in list
  • frequency list python
  • make a list for frequency and number in python
  • freuency of a list python
  • how to get frequency in python
  • frequency of values python
  • use switch to count frequency python
  • finding elements with equal frequency as its value in list python
  • calculating frequency of all elements of list in python
  • count frequency of numbers in a list
  • counting the frequency of elements inside a list using .setdefault()
  • write a python program to get the frequency of the elements in a list
  • python count frequency
  • python count item frequency in list
  • python count number frequency in list
  • number frequency counter python
  • count frequency python
  • python count value frequency in list
  • count frequency of element in list using list.count python without using dictionary
  • how to count frequency of elements in a list in python 3
  • count frequency of values list
  • counting frequency of an element in python
  • python array counter frequency
  • how to check the frequency of a number in a list
  • python list count frequency
  • python count frequency of values in list
  • frequency number in list
  • list of frequency python
  • a list that count frequency of a element in python
  • python frequency counter list
  • best way to count frequency in python list
  • how to count the frequency of an element in a list
  • count frequency of each item in list
  • how to count item frequency in python
  • python set count elements
  • count frequency in list python
  • frequency table of a list in python
  • python program to count the frequency of elements in a list using a dictionary
  • python program using add() method to find histogram to count the number of times each letter apppears in a word and in sentence
  • python frequency count of list
  • count python
  • how to count repeated values in list of dictionaries java
  • how to find frequency in python
  • frequency of numbers in list python
  • method count in python
  • number of occurrences each item in list to dic in python
  • how to count in python
  • count the frequency of each element of a list python
  • frequency dict python
  • list frequency python
  • how to generate a dictionatry that counts the frequency of items in a list in python
  • python dictionary for frequency
  • frequency of values in list python
  • frequency count dictionary python
  • frequency element python list
  • count frequency of words in list python
  • python group list count
  • frequency count in python
  • frequency using dictionary in python
  • count number frequency python
  • python get frequency of list
  • frequency of list elements python
  • frequency of a number in a list python
  • frequency of an element in a list python
  • python frequency in list
  • python count list frequency
  • python get frequency of list items
  • count frequency of element in list python
  • count frequency of numbers in list python
  • frequency in python
  • count fro all elements in python
  • frequency of occurrences python
  • count feqcuyency of differents list in python
  • display frequencies of words in an array using dictionaries python
  • frequency count in list in python using function
  • frequency program python
  • frequency of occurences in a list python
  • count() in python
  • how to get frequency of list in python
  • freqency in python
  • element frequency list python
  • how to count frq value in dictionary python
  • find the occurence of items in a collection of lists using dictionary
  • how to add count of an element in dictionary in python
  • list count python
  • overall disturbution of string python
  • python frequency using dict.get
  • counting with dictionaries python
  • count using dictionary python
  • convert array of count ofnumbers to dictionary in python
  • value frequency in python
  • python create frequency dict using list
  • find element in list python with given frequency
  • list count in dictionary python
  • sql count rows python
  • how to check frequency is even iin dict
  • how to count occurence of all the elements in a list using dict in python
  • python count letter frequency in string
  • match keys and increse frequency in python dioctionary
  • fraquencies of the list python
  • count frequency of elements in series python
  • how to convert a list to dict with count of repetitions
  • list.count python
  • dictionary of all count of each occurences in list
  • create a dictionary that shows the frequency of elements inside a list of python
  • how to convert a list of integers to a dictionary frequency python
  • colections .count python
  • counting using dictionaries
  • python count character frequency in string
  • python frequency count list
  • write a python program to get the frequency of the elements in a list without using dictionary
  • check frequency of number python
  • count in dictionary python
  • counting frequnecies of elements in a list in python
  • frequency of elements in list python
  • list get frequency
  • python get frequency
  • python frequency counter of list
  • count word frequency python
  • get the frequency of the elements in a list python
  • frequency of values in a list
  • python list count equal elements
  • python frequency of elements in list
  • how to make set of unique elements from freq count in python
  • how to count frequency of list in python
  • get frequency list python
  • python find frequency of element in list
  • how to find the the most frequency word use in a dictionaryin python
  • list duplicate frequency in array python
  • frequency table from list python
  • understanding count frequency in python
  • list element frequency python
  • frequency counting python
  • count object frequency in list
  • frequency counter python
  • python check frequency of numbers
  • count frequency using list comprehension python
  • fastest to count frequency in list oyhton
  • count frequency of element in list using list.count python using for loop
  • count frequency of a list python
  • frequency table from list counter python
  • plot of values frequency in list python
  • how to count list data frequency in python
  • how to use frequency in list
  • python calculate frequency in list
  • find frequency in list python
  • frequency of list items python
  • count frequency of values in list python
  • count frequency from list
  • how to count the frequency of an number on the list
  • python list frequency counter
  • how to count the frequency of an element in a list python
  • how to count frequency in list python
  • python count frequency

Python: Count the frequency in a given dictionary

Last update on April 27 2021 12:48:12 (UTC/GMT +8 hours)

What will we cover?

We will learn about dictionaries and how to use them for frequency count.

See the full tutorial on Python Dictionaries as well.

Video liên quan

Postingan terbaru

LIHAT SEMUA