11 Ways to Work with Lists of Dictionaries in Python

Claudio Sabato
7 min readApr 13, 2023

--

Are you creating a Python program that requires a list of dictionaries and you don’t know how to do it? You will learn how in this tutorial.

A list of dictionaries is a Python list in which every element is a dictionary. Using list methods you can execute operations like creating a brand new list of dictionaries, appending a dictionary to a list, updating a dictionary in a list, or deleting a dictionary from a list. You can also use list comprehensions to search for a dictionary in a list based on the value of a dictionary key.

Let’s learn how to work with this data structure!

How to Create a List of Dictionaries in Python

The first thing we will learn about lists of dictionaries is how to create them.

Imagine you are creating an application in which you have to track multiple users and each user has several properties (e.g. name, surname, and age). You could use a list of dictionaries as a data type to handle this requirement.

The properties related to one user can be represented with the following dictionary:

user1 = {'name': 'John', 'surname': 'Red', 'age': 27}

To track the properties of multiple users you can define a list of dictionaries:

users = [
{'name': 'John', 'surname': 'Red', 'age': 27},
{'name': 'Kate', 'surname': 'Green', 'age': 40},
{'name': 'Jack', 'surname': 'Brown', 'age': 32}
]

Notice that we use the comma to separate items in the list that in this case are dictionaries.

Let’s print the users variable and confirm its type using the type() built-in function:

print(users)

[output]
[{'name': 'John', 'surname': 'Red', 'age': 27}, {'name': 'Kate', 'surname': 'Green', 'age': 40}, {'name': 'Jack', 'surname': 'Brown', 'age': 32}]

print(type(users))

[output]
<class 'list'>

We will also use a for loop to confirm that each element of the list is a dictionary:

for user in users:
print(type(user))

[output]
<class 'dict'>
<class 'dict'>
<class 'dict'>

How to Add a Dictionary to a List of Dictionaries

To add a dictionary to a list of dictionaries you can use the list append() method. This is the same approach you would use with any Python list.

Let’s add a new user to the users list and then print the updated list.

user = {'name': 'Frank', 'surname': 'Black', 'age': 36}
users.append(user)
print(users)

[output]
[{'name': 'John', 'surname': 'Red', 'age': 27}, {'name': 'Kate', 'surname': 'Green', 'age': 40}, {'name': 'Jack', 'surname': 'Brown', 'age': 32}, {'name': 'Frank', 'surname': 'Black', 'age': 36}]

You can see that the dictionary has been added to the list.

How to Print a List of Dictionary Without Brackets

In the previous section, we printed the value of a list of dictionaries. The output shows open and closed square brackets and within those, all the dictionaries are separated by commas.

To print a list of dictionaries without brackets you can use a Python for loop.

for user in users:
print(user)

[output]
{'name': 'John', 'surname': 'Red', 'age': 27}
{'name': 'Kate', 'surname': 'Green', 'age': 40}
{'name': 'Jack', 'surname': 'Brown', 'age': 32}
{'name': 'Frank', 'surname': 'Black', 'age': 36}

This makes the output a lot more readable also because you see the dictionaries one under the other.

How to Access Values in a List of Python Dictionaries

A common requirement is accessing specific dictionary values within a list of dictionaries.

To access values in a list of dictionaries you can access a specific dictionary using the index of a dictionary in the list. Then use square brackets to access a specific value mapped to a dictionary key.

list_name[list_index][dictionary_key]

For example, how can you access the name attribute in the first dictionary of the list?

print(users[0]['name'])

[output]
John

To access the first dictionary in a list of dictionaries you have to use the index zero because the index of a list goes from 0 to n-1 where n is the number of elements in the list.

How Can You Update a Dictionary Value in a List of Dictionaries?

Imagine you want to update the age of one of the users. How can you do that?

To update the value mapped to a dictionary key in a list of dictionaries you can use the following Python syntax:

list_name[list_index][dictionary_key] = new_value

Let’s update the age of the user Frank to 37.

users[3]['age'] = 37
print(users)

[output]
[{'name': 'John', 'surname': 'Red', 'age': 27}, {'name': 'Kate', 'surname': 'Green', 'age': 40}, {'name': 'Jack', 'surname': 'Brown', 'age': 32}, {'name': 'Frank', 'surname': 'Black', 'age': 37}]

You can see that the dictionary value has been updated to the new value.

How to Add a Key to a Dictionary in a List of Dictionaries

What if you want to add a new key to all the users in your application?

To add a new key value pair to an existing dictionary in a list of dictionaries you have to access the dictionary in the list using an index and then set the new key : value. If you want to add the new key to every dictionary in the list you can use a for loop. In that case, you don’t need to use the list index to access each dictionary because this is handled by the internal logic of the Python for loop.

list_name[list_index][new_dictionary_key] = value

For example, you might want to add the full name by concatenating name and surname and associating the value to a new dictionary key called “fullname”.

You can use a for loop that goes through each dictionary in the list and set the value for the key “fullname”.

for user in users:
user['fullname'] = f"{user['name']} {user['surname']}"

Print all the elements in the list to confirm that the new key has been added to each dictionary:

print(f"Updated list with fullname:")

for user in users:
print(user)

[output]
Updated list with fullname
{'name': 'John', 'surname': 'Red', 'age': 27, 'fullname': 'John Red'}
{'name': 'Kate', 'surname': 'Green', 'age': 40, 'fullname': 'Kate Green'}
{'name': 'Jack', 'surname': 'Brown', 'age': 32, 'fullname': 'Jack Brown'}
{'name': 'Frank', 'surname': 'Black', 'age': 37, 'fullname': 'Frank Black'}

It worked!

How Can You Delete a Dictionary from a List of Dictionaries?

Following a similar principle to append a dictionary to a list, we can also delete a dictionary from a list.

To delete a dictionary from a list of dictionaries you can use the Python del statement.

del list_name[list_index]

Let’s delete the second dictionary in our list:

del users[1]
print(f"Updated list after deleting the second dictionary:")

for user in users:
print(user)

[output]
Updated list after deleting the second dictionary:
{'name': 'John', 'surname': 'Red', 'age': 27, 'fullname': 'John Red'}
{'name': 'Jack', 'surname': 'Brown', 'age': 32, 'fullname': 'Jack Brown'}
{'name': 'Frank', 'surname': 'Black', 'age': 37, 'fullname': 'Frank Black'}

The updated list now contains only three elements instead of four because we have deleted one dictionary from it.

How Do You Create an Empty Python List of Dictionaries?

There isn’t a specific way to create an empty list of dictionaries. You would simply create an empty list using the following syntax:

list_name = []

Then add dictionaries to the list by using the list append() method.

You could create a Python list that contains an empty dictionary but this doesn’t make a lot of sense. Here is how you can do it:

user = dict()
users = [user]
print(users)

[output]
[{}]

Now you can add a key-value pair to the empty dictionary in the list:

users[0]['name'] = 'Jack'
print(users)

[output]
[{'name': 'Jack'}]

How to Search For a Dictionary Key-Value in a List of Dictionaries Using a List Comprehension

A very common use case is looking for a dictionary in a list of dictionaries based on the value of one of the keys.

For example, how can you get the dictionary for the user ‘Frank’ from our list?

users = [
{'name': 'John', 'surname': 'Red', 'age': 27},
{'name': 'Kate', 'surname': 'Green', 'age': 40},
{'name': 'Jack', 'surname': 'Brown', 'age': 32},
{'name': 'Frank', 'surname': 'Brown', 'age': 36}
]

One way to do that is by using a list comprehension:

search_result = [user for user in users if user['name'] == 'Frank']
print(search_result)

[output]
[{'name': 'Frank', 'surname': 'Brown', 'age': 36}]

This returns a list that contains the dictionaries matching the specific key-value.

Searching For a Dictionary in a List of Dictionaries Using a Generator Expression

You can search for a dictionary in a list of dictionaries based on the value of a key using a generator expression.

The code is almost identical to the one in the previous section where we used a list comprehension.

The first difference is that we will replace the square brackets with parentheses to create the generator expression.

search_result = (user for user in users if user['name'] == 'Frank')
print(search_result)

[output]
<generator object <genexpr> at 0x104634110>

To get the value from the generator expression you can use Python’s next() function:

print(next(search_result))

[output]
{'name': 'Frank', 'surname': 'Brown', 'age': 36}

This time you can see that we get back the dictionary without including the open and closed square brackets returned by the list comprehension.

How to Find a Dictionary in a List of Dictionaries Using Filter and Lambda Functions

An alternative way to identify a dictionary in a list based on a dictionary value is by using the filter function and a lambda function. You can use the generic syntax below:

filter(lambda_function, list_of_dictionaries)

Let’s apply this in practice:

search_result = filter(lambda user: user['name'] == 'Frank', users)
print(search_result)

[output]
<filter object at 0x100efbe50>

You can use the next function to get back the list that matches our search criteria:

print(next(search_result))

[output]
{'name': 'Frank', 'surname': 'Brown', 'age': 36}

If instead of using the next() function you use the list() built-in function you get back a list that contains the dictionary that matches the search criteria:

print(list(search_result))

[output]
[{'name': 'Frank', 'surname': 'Brown', 'age': 36}]

Conclusion

After reading this article and practicing all the exercises we went through you will be able to work with lists of dictionaries and use them in your programs.

You know how to create a list of dictionaries, add a dictionary to it, add a new dictionary key, and delete a dictionary from the list.

Also, if you have to find a dictionary in the list based on specific search criteria, you know multiple ways to do it.

Related article: in this Python tutorial we have used the next() function. Read the Codefather article that explains how the Python next() function works.

--

--

Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.