List of dictionaries in Python(with Examples)

List of dictionaries in Python

Hello Techies,

In this tutorial, we will learn about how to create, access, and use of List of dictionaries in Python with the help of examples.

What is a List of Dictionaries in Python

The list of Dictionaries in Python is to add a Dictionary to the list.

Example: list_of_dict = [{“a”: 1, “b”: 2, “c”: 3}, {“d”: 5, “e”: 6, “f”: 7, “g”: 8}]


Append method to create a list of dictionaries in Python

Creating a list of dictionaries in Python is easy. You need to use the append method to create lists.

Example 1: To create a list of lists in Python.

# Program to create a list of dictionaries in Python

dict1 = {"a": 1, "b": 2, "c": 3}
dict2 = {"d": 5, "e": 6, "f": 7, "g": 8}

# create empty list
list_of_dict = []
list_of_dict.append(dict1)
list_of_dict.append(dict2)
print("List of Dict:", list_of_dict)

Output:

List of Dict: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7, 'g': 8}]

Using the append() method you also update the new dictionary to a list in Python.

Example 2: To update the new dictionary to a list in Python

# Program to update new dictionary to list

list1 = [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}]
print("Original Dictionary is:", list1)

# update new dictionary to list
list1.append({'g': 8, 'h': 9, 'i': 10})
print("Updated dictionary is:", list1)

Output:

Original Dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}]
Updated dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]

How to access key: value pairs in a list of Dictionaries in Python

We know we can add a variety of items to a list, and you need to use an index when you want to access those elements, so you need to use a list index with a dictionary key to access any dictionary key: value pair from the list.

Example1: To access key: value pairs in a list of Dictionaries

# Program to access a list of dictionaries in Python

list1 = [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
print("Original Dictionary is:", list1)

# access 2nd dictionary from list
print("2nd dictionary is:", list1[1])

# access key d from 2nd dictionary from list
print("value of d key from 2nd dictionary:", list1[1].get('d'))

Output:

Original Dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
2nd dictionary is: {'d': 5, 'e': 6, 'f': 7}
value of d key from 2nd dictionary: 5

If you check in to the above program, instead of the get() method, You can also code (list1[1][‘d’]) like this, but the advantage of the get() method is that if you pass a non-existent key in it, it will not raise an error but if you code (list1[1] [‘Y’]) like this, then it will raise the KeyError.

Check out the below program:

# Program to access a list of dictionaries in Python

list1 = [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
print("Original Dictionary is:", list1)

# access 2nd dictionary from list
print("2nd dictionary is:", list1[1])

# access key d from 2nd dictionary from list
# using get() method
print("value of d key from 2nd dictionary"
      "(using get() method):", list1[1].get('y'))

# access key d from 2nd dictionary from list
# without using get() method
print("value of d key from 2nd dictionary"
      "(without using get() method):", list1[1]['y'])

Output:

Original Dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
2nd dictionary is: {'d': 5, 'e': 6, 'f': 7}
value of d key from 2nd dictionary(using get() method): None
Traceback (most recent call last):
  File "C:\Users\a\Desktop\test\test1.py", line 17, in <module>
    "(without using get() method):", list1[1]['y'])
KeyError: 'y'

Example 2: Python list of dictionaries get value using list comprehension

# Get values of a particular key in the list of dictionaries 
# Using list comprehension

list1 = [{'a': 1, 'b': 2, 'c': 3},  {'a': 12},
         {'d': 3, 'a': 5}]
print("Original list of dictionary is:", list1)

# Using list comprehension
# Get values of particular key in list of dictionaries
result = [each['a'] for each in list1]
print("Key related values:", result)

Output:

Original list of dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'a': 12}, {'d': 3, 'a': 5}]
Key related values: [1, 12, 5]

Example 3: Python list of dictionaries get value using list comprehension

# Get values of particular key in list of dictionaries
# Using map() + itemgetter()

from operator import itemgetter
list1 = [{'a': 1, 'b': 2, 'c': 3},  {'a': 12},
         {'d': 3, 'a': 5}]
print("Original list of dictionary is:", list1)

# Using map() + itemgetter()
# Get values of particular key in list of dictionaries
result = list(map(itemgetter('a'), list1))
print("Key related values:", result)

Output:

Original list of dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'a': 12}, {'d': 3, 'a': 5}]
Key related values: [1, 12, 5]

Python update dictionary value by key in List of Dictionaries

Check the below program on How to update dictionary value by key in List of Dictionaries.

# Program to update a list of dictionaries in Python

list1 = [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
print("Original Dictionary is:", list1)

# update key d from 2nd dictionary from list
list1[1]['d'] = 70
print("Updated dictionary is:", list1)

Output:

Original Dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
Updated dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 70, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]

Update values of a list of dictionaries in Python

If you want to update any values of the list of dictionaries you can use the below method.

# Program to update values of
# list of dictionaries in Python

lsit1 = [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
print("Original Dictionary is:", lsit1)

# update key d from 2nd dictionary from list
lsit1[1]['d'] = 70
print("Updated dictionary is:", lsit1)

Output:

Original Dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 5, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]
Updated dictionary is: [{'a': 1, 'b': 2, 'c': 3}, {'d': 70, 'e': 6, 'f': 7}, {'g': 8, 'h': 9, 'i': 10}]

Iterate through a list of dictionaries in Python

Example: Python loop through a list of dictionaries

# Program for Iterate through a
# list of dictionaries in Python

list1 = [{'a': 1, 'b': 2, 'c': 3},  {'a': 12},
         {'d': 3, 'a': 5}]

# using for loop: Iterate through a
# list of dictionaries in Python
for each in list1:
    for k, v in each.items():
        print('{}: {}'.format(k, v))

Output:

a: 1
b: 2
c: 3
a: 12
d: 3
a: 5

FAQ on List of dictionaries in Python

  1. How do you define a list of dictionaries in Python?

    The list of Dictionaries in Python is to add a Dictionary to the list.
    For example, ( list_of_dict = [{“a”: 1, “b”: 2, “c”: 3}, {“d”: 5, “e”: 6, “f”: 7, “g”: 8}]) this way we can define a list of dictionaries in Python.

  2. How do I find a dictionary in a list?

    Using for loop or indexing we can find a dictionary in a list.
    For eg,
    1)Using Index
    >>> list_of_dict = [{“a”: 1, “b”: 2, “c”: 3}, {“d”: 5, “e”: 6, “f”: 7, “g”: 8}]
    >>> list_of_dict[1]
    {‘d’: 5, ‘e’: 6, ‘f’: 7, ‘g’: 8}

    2)Using For loop
    list_of_dict = [{“a”: 1, “b”: 2, “c”: 3}, {“d”: 5, “e”: 6, “f”: 7, “g”: 8}]
    for each in list_of_dict:
    … print(each)
    … for k, v in each.items():
    … print(k, v)

    {‘a’: 1, ‘b’: 2, ‘c’: 3}
    a 1
    b 2
    c 3
    {‘d’: 5, ‘e’: 6, ‘f’: 7, ‘g’: 8}
    d 5
    e 6
    f 7
    g 8

  3. How do I create a list of dictionaries?

    First create a empty list and append a dictionary to the empty list.

  4. How do I iterate a list of dictionaries in Python?

    You can use the for loop for list iteration. Then use the item() function for both key and values for dictionary iteration.


Conclusion

In this tutorial, you learned about a list of dictionaries in Python with the help of multiple examples, We have covered the following points:

  • What is a List of Dictionaries in Python
  • Append method to create a list of dictionaries in Python
  • How to access key: value pairs in a list of Dictionaries in Python
  • Python update dictionary value by key in List of Dictionaries
  • Update values of a list of dictionaries in Python
  • Iterate through a list of dictionaries in Python

Leave a Comment