In this python dictionary program, we will extract a list of values from a given list of dictionaries
Steps to solve the program
- Take a list of dictionaries as input and create an empty list.
- First, iterate over dictionaries in the list using a for loop.
- Now to iterate over the keys and values of each dictionary use another for loop.
- Add the values to the empty list for the specified key.
- 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]