Python: Count number of items in a dictionary value that is a list
Write a Python program to count the number of items in a dictionary value that is a list.
Visual Presentation:
Sample Solution:
Python Code:
# Create a dictionary 'dict' with names as keys and lists of subjects as values.
dict = {'Alex': ['subj1', 'subj2', 'subj3'], 'David': ['subj1', 'subj2']}
# Calculate the total number of subjects by summing the lengths of the lists (values) in the dictionary.
ctr = sum(map(len, dict.values()))
# Print the total count of subjects.
print(ctr)
Sample Output:
5
Python Code Editor:
Previous: Write a Python program to check multiple keys exists in a dictionary.
Next: Write a Python program to sort Counter by value.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics