Find out frequency of unique numbers in a list

Frequency of unique number is one such question that almost every python programmer try to solve. We will solve this problem by using a list and a python dictionary.

The program, first of all, generates a list of n number from keyboard and then it will find out the frequency of all the unique element.

Source code to find out the frequency of unique numbers

#program to create a list of n numbers from  keyboard and create a frequency table for all unique number
#   made by         : rakesh kumar

numbers = []
n = int(input('Enter value of n :'))
for i in range(n):
    x = int(input('Enter any number :'))
    numbers.append(x)

unique = []
frequency = []

for a in numbers:
    if a not in unique:
        unique.append(a)
        count = numbers.count(a)
        frequency.append(count)

# print unique numbers and frequency table
print('Unique Number\t\t Frequency')
for i in range(len(unique)):
    print(unique[i], frequency[i], sep='\t\t')

In the above program, we use two lists to store unique numbers and frequency of each such unique numbers. Besides these not in container operator and count() method to find out frequency.

The output of the above program

$ C:/Python37/python.exe c:/Users/rakesh/Desktop/table.py
Enter value of n :6
Enter any number :12
Enter any number :34
Enter any number :5
Enter any number :6
Enter any number :5
Enter any number :6
12              1
34              1
Enter any number :3
Unique Number            Frequency
12              1
1               1
2               1
34              1
4               1
5               1
56              1
3               1

Extra Challenge: Modify the above program so that it can be used on any type of data available in the list ie heterogeneous type of data.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.