Count a number of items in a dictionary value that is in a list.

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
  1. Take a dictionary as input and create a variable and assign its value equal to 0.
  2. Use for loop to iterate over values of the dictionary.
  3. Use a nested for loop to iterate over the value list.
  4. Add 1 to the variable for each item in the value list.
  5. 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
				
			

convert a list of dictionary to a list of lists.

sort items in a dictionary in descending order.

Leave a Comment