Write a Python program to count number of items in a dictionary value that is a list


This program demonstrates how to count the total number of items present in a dictionary, which is a collection of key-value pairs. The dictionary "d" is initialized with three keys "M1", "M2", and "M3" and their respective values, which are lists of integers.

The sum function with the map function is used to calculate the total number of items in the dictionary. The map function is applied to the values of the dictionary, and the len function is applied to each value to get the number of items in each list. The result of map is then passed to sum to get the total number of items in the dictionary. The final result is displayed using the print statement.

Source Code

d =  {'M1': [67,79,90,73,36], 'M2': [89,67,84], 'M3': [82,57]}
tot = sum(map(len, d.values()))
print("Number of Items in a Dictionary :",tot)

Output

Number of Items in a Dictionary : 10