Count the occurrence of each element from a list
380
Write a program to iterate a given list and count the occurrence of each element and create a dictionary to show the count of each element.
Given Input:
sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89]
Expected Output:
Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Program:
sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89]
print("Original list ", sample_list)
count_dict = dict()
for item in sample_list:
if item in count_dict:
count_dict[item] += 1
else:
count_dict[item] = 1
print("Printing count of each item ", count_dict)
Output:
Original list [11, 45, 8, 11, 23, 45, 23, 45, 89]
Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Explanation:
None
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS
Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Program:
sample_list = [11, 45, 8, 11, 23, 45, 23, 45, 89] print("Original list ", sample_list) count_dict = dict() for item in sample_list: if item in count_dict: count_dict[item] += 1 else: count_dict[item] = 1 print("Printing count of each item ", count_dict)
Output:
Original list [11, 45, 8, 11, 23, 45, 23, 45, 89] Printing count of each item {11: 2, 45: 3, 8: 1, 23: 2, 89: 1}
Explanation:
None
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS