KnowledgeBoat Logo

Computer Science

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

Python

Python Dictionaries

2 Likes

Answer

my_dict = eval(input("Enter the dictionary: "))
total_count = 0
for value in my_dict.values():
    if type(value) is list:
        total_count += len(value)
print("Total number of items in lists within the dictionary:", total_count)

Output

Enter the dictionary: {'l1': [1, 2, 3, 4], 'l2': 'Apple', 'l3': [True, False], 'l4': 'Orange'}         
Total number of items in lists within the dictionary: 6

Answered By

1 Like


Related Questions