Write a Python Program to Count The Frequencies in a List Using Dictionary

Given a string array input to your Python program your task is to count all the frequency of occurrences of individual list items and store them in the dictionary.

The dictionary will store the key as a list item and the number of occurrences of each item that is a frequency will be stored as a value.

Upon the frequency of occurrences of each character list item is known then print the dictionary. 

 

 

Write a Python Program to Count The Frequencies in a List Using Dictionary



Write a Python Program to Count The Frequencies in a List Using Dictionary

Take an integer variable and ask a user to enter the list size using input function call,  then take an empty list of numbers And using the  for loop  and arrange it from 0 to this size ask a user to enter the integer.  what is the user enters an integer appended to the  list of numbers.

n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter List Item (Integer) : "))
   numbers.append(temp)
 



Take an empty dictionary named as freq, Iterate all the items present in the list of numbers using the if statement check item is already present in the freq dictionary,  if already present increment the value else assign 1 as the value.

freq = {}
for item in numbers:
   if item in freq:
       freq[item] += 1
   else:
       freq[item] = 1




Using the for loop, iterate all the  key value pairs present in the dictionary and print the key as well as the frequency of occurrence that is a value of the dictionary using print statement.

for key, value in freq.items():
   print(f"{key} occurs {value} times")




Complete  Python working code
===============================

n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
   temp = int(input("Enter List Item (Integer) : "))
   numbers.append(temp)

freq = {}
for item in numbers:
   if item in freq:
       freq[item] += 1
   else:
       freq[item] = 1

for key, value in freq.items():
   print(f"{key} occurs {value} times")




OUTPUT
===================

Enter List Size : 5
Enter List Item (Integer) : 1
Enter List Item (Integer) : 2
Enter List Item (Integer) : 3
Enter List Item (Integer) : 4
Enter List Item (Integer) : 4
1 occurs 1 times
2 occurs 1 times
3 occurs 1 times
4 occurs 2 times




OUTPUT 2
====================

Enter List Size : 12
Enter List Item (Integer) : 44
Enter List Item (Integer) : 23
Enter List Item (Integer) : 11
Enter List Item (Integer) : 34
Enter List Item (Integer) : 56
Enter List Item (Integer) : 78
Enter List Item (Integer) : 90
Enter List Item (Integer) : 33
Enter List Item (Integer) : 23
Enter List Item (Integer) : 44
Enter List Item (Integer) : 78
Enter List Item (Integer) : 22
44 occurs 2 times
23 occurs 2 times
11 occurs 1 times
34 occurs 1 times
56 occurs 1 times
78 occurs 2 times
90 occurs 1 times
33 occurs 1 times
22 occurs 1 times



Conclusion
===================

Execute the above program by modifying from the integer to a floating point value,  note down the results.

Comment down below if you have any suggestions or queries regarding the program


Post a Comment

0 Comments