Python: Count number of items in a dictionary value that is a list
Python dictionary: Exercise-34 with Solution
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-34.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics