How to iterate over dictionaries in a Python list?

In Python, you can use loops to iterate through dictionaries in a list. Here are two common ways to do it:

  1. Use a for loop to iterate through dictionaries in a list.
my_list = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]

for item in my_list:
    for key, value in item.items():
        print(key, value)
  1. Iterate through a list of dictionaries using indexing and loops.
my_list = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]

for i in range(len(my_list)):
    for key, value in my_list[i].items():
        print(key, value)

Both of the methods mentioned can be used to print the keys and values of the dictionary, allowing you to perform operations based on your individual needs.

bannerAds