35. Problem to get keys and values from the list of dictionaries.

In this program, we will take input as a list of dictionaries. Get the keys and values from the list of dictionaries using for loop with the help of the below-given steps.

Get keys and values from the list of dictionaries:

  1. Take a list of dictionaries as input.
  2. Create two empty lists named key and value to store keys and values.
  3. Use for loop iterate over the list of a dictionary.
  4. Use another for loop inside the above loop to add the key and value of the dictionary element into the empty list.
  5. Print both lists to see the output.
				
					#Input list
list1 = [{"a":12}, {"b": 34}, {"c": 23}, {"d": 11}, {"e": 15}]

#Creating empty list
key = []
value = []
for element in list1:
    for val in element:
        key.append(val)
        value.append(element[val])

#Print output
print(key)
print(value)
				
			

Output :

				
					['a', 'b', 'c', 'd', 'e']

[12, 34, 23, 11, 15]
				
			

Related Articles

Combine two list elements as a sublist

Get all the unique numbers in the list

Leave a Comment