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:
- Take a list of dictionaries as input.
- Create two empty lists named key and value to store keys and values.
- Use for loop iterate over the list of a dictionary.
- Use another for loop inside the above loop to add the key and value of the dictionary element into the empty list.
- 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
Python program to get all the unique numbers in the list.
Python program to convert a string into a list.
Python program to replace the last and the first number of the list with the word.
Python program to check whether the given element is exist in the list or not.
Python program to remove all odd index elements.
Python program to take two lists and return true if then at least one common member.