Append Element to List in Dictionary in Python (3 Examples)

 

In this Python programming tutorial, you’ll learn how to append elements to a list in dictionaries.

Table of contents:

Let’s take a look at some Python codes in action.

 

Exemplifying Data

We use the following data as a basis for this tutorial:

my_dict = {'list1': [1, 2, 3], 'list2': [4, 5, 6]}  # create sample dictionary
print(my_dict)                                      # print my_dict

It is a dictionary containing two lists named list1 and list2. Without further ado, let’s jump into the examples!

 

Example 1: Add Element to Dictionary List

If one wants to append an element to a list, she should first access the list associated key of interest, which is list1 in this case, through my_dict['list1']. Then using the append method, the new element can be added.

my_dict['list1'].append(10)                         # append to list1 in my_dict
print(my_dict)                                      # print updated my_list
# {'list1': [1, 2, 3, 10], 'list2': [4, 5, 6]}

As you can see, based on the previous Python console, number 10 is added to list1 in my_dict. If your interest is adding multiple elements, you should visit the following example.

 

Example 2: Add Multiple Elements to Dictionary List

In this example, we will add the elements 20 and 30 to list1 in my_dict. This will be achieved by the extend method as follows.

my_dict['list1'].extend([20, 30])                   # append to list2 and list3
print(my_dict)                                      # print updated my_list
# {'list1': [1, 2, 3, 10, 20, 30], 'list2': [4, 5, 6]}

You can see above how [20, 30] are successfully added to list1. What if we aim to add elements to a nonexisting list? Check out the next example for the demonstration!

 

Example 3: Add Elements to Missing Dictionary List

In the scenario of a missing dictionary key, we need to set an if statement to ensure that the new elements are added to the dictionary regardless of the presence of the list of interest. Let’s take a look at the following script.

key = 'list3'                                       # name list to be added
value = [7, 8, 9]                                   # define values
 
if key in my_dict:                                  # append to (non)existing list3
    my_dict[key].extend(value)
else:
    my_dict[key] = value
 
print(my_dict)                                      #print updated my_dict
# {'list1': [1, 2, 3, 10, 20, 30], 'list2': [4, 5, 6], 'list3': [7, 8, 9]}

As seen above, the goal was to add the values 7, 8, and 9 to list3. The if statement first checked if the key "list3" existed in my_dict, then accordingly, it either extended the existing list or set a new key "list3", which was assigned to the value through my_dict[key] = value. In this case, the else condition held.

We have shown two main appending methods: append and extend, in this tutorial. However, appending elements to lists is not limited to these two methods. You can see the tutorial: Append Integer to List in Python for other alternatives.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the Python programming syntax of this tutorial. You can find the video below:

 

The YouTube video will be added soon.

 

Besides that, you might read the other articles on this homepage. I have published several articles on topics such as data elements, indices, and data conversion.

 

Summary: You have learned in this tutorial how to extend lists in dictionaries in Python. If you have further questions, please let me know in the comments section below.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on 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