Add Keys to List of Dictionaries in Python (3 Examples)

 

This tutorial will show different ways to add a key to each dictionary in a list in the Python programming language.

The article will follow this structure:

Let’s dive into the code!

 

Sample Data

We will start this tutorial by creating a sample list of dictionaries as follows.

my_list = [{'name': 'Claire', 'age': 26}, 
           {'name': 'John', 'age': 28}]

As shown, my_list is a list object that contains two dictionaries. Let’s now define a dictionary new_keys of which key and value pairs will be added to my_list!

new_keys = {'country': 'United Kingdom', 'city': 'London'}

Let’s take a look at the following examples.

 

Example 1: Join New Keys to List of Dictionaries Using for Loop & update()

A for loop and the update() method can be handy when adding new keys to a list of dictionaries. Check out the following code.

for d in my_list:
    d.update(new_keys)
print(my_list)
# [{'name': 'Claire', 'age': 26, 'country': 'United Kingdom', 
# 'city': 'London'}, {'name': 'John', 'age': 28, 
# 'country': 'United Kingdom', 'city': 'London'}]

We’ve just shown how a for loop iterates over each dictionary d in my_list. For each dictionary, it calls the update method, which adds any new key-value pairs in new_keys to d. Normally, it also updates the values of any existing keys in d with the corresponding values in new_keys; however, it is not the case in this example.

 

Example 2: Join New Keys to List of Dictionaries Using List Comprehension & update()

It’s also possible to join new keys to a list of dictionaries using the list comprehension technique and the update method.

[d.update(new_keys) for d in my_list]
print(my_list)
# [{'name': 'Claire', 'age': 26, 'country': 'United Kingdom', 
# 'city': 'London'}, {'name': 'John', 'age': 28, 
# 'country': 'United Kingdom', 'city': 'London'}]

This time, the list comprehension iterates over each dictionary d in my_list and calls the update method with the argument new_keys. We obtain just the same result as in the previous example.

 

Example 3: Join New Keys to List of Dictionaries Using for Loop & Bracket Notation

This example shows how to add keys to a list of dictionaries using a for loop and a bracket notation. Let’s take a look!

for d in my_list:
    for key, value in new_keys.items():
        d[key] = value
print(my_list)
# [{'name': 'Claire', 'age': 26, 'country': 'United Kingdom', 
# 'city': 'London'}, {'name': 'John', 'age': 28, 
# 'country': 'United Kingdom', 'city': 'London'}]

This time, when the for loop iterates over each dictionary in my_list, and the second for loop sets the key and value from new_keys to the dictionaries d in my_list via bracket notation.

 

Video, Further Resources & Summary

Do you need more explanations on how to add new elements to a list of dictionaries in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

There are some other tutorials on Statistics Globe you may be interested in:

This post has shown how to add keys to a list of dictionaries in Python. In case you have further questions, please let me know in the comments.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top