Extract a list of values from a given list of dictionaries

In this python dictionary program, we will extract a list of values from a given list of dictionaries

Steps to solve the program
  1. Take a list of dictionaries as input and create an empty list.
  2. First, iterate over dictionaries in the list using a for loop.
  3. Now to iterate over the keys and values of each dictionary use another for loop.
  4. Add the values to the empty list for the specified key.
  5. Print the output.
				
					D1 = [{'t20':50,'odi':70},{'t20':40,'odi':10},{'t20':30,'odi':0},{'t20':45,'odi':65}]
l = []

for ele in D1:
    for key,val in ele.items():
        if key == 'odi':
            l.append(val)
            
print(l)
				
			

Output :

				
					[70, 10, 0, 65]
				
			

update the list values in the said dictionary.

find the length of dictionary values.

Leave a Comment