In this python dictionary program, we will count a number of items in a dictionary value that is in a list.
Steps to solve the program
- Take a dictionary as input and create a variable and assign its value equal to 0.
- Use for loop to iterate over values of the dictionary.
- Use a nested for loop to iterate over the value list.
- Add 1 to the variable for each item in the value list.
- Print the output.
dict1 = {'virat':['match1','match2','match3'],'rohit':['match1','match2']}
count = 0
for val in dict1.values():
if type(val)==list:
for ele in val:
count += 1
print("Items in the list of values: ",count)
Output :
Items in the list of values: 5