Append Dict to List in Python (4 Examples)
In this Python tutorial, you’ll learn how to append dictionaries to a list using different methods.
The article contains these contents:
Let’s get started!
Introduce Example Data
First, I will create an empty list to add the sample dictionaries in the following examples. Needless to say, the sample list could also be a non-empty list.
my_list = [] # Initialize an empty list print(my_list) # Print the list # []
The sample list, my_list
, has been created. Now, we can create our sample dictionaries as follows.
dict1 = {"key1": "value1", "key2": "value2"} # Create a dictionary print(dict1) # {'key1': 'value1', 'key2': 'value2'} # Print the dictionary dict2 = {"key3": "value3", "key4": "value4"} # Create a sec dictionary print(dict2) # {'key3': 'value3', 'key4': 'value4'} # Print the dictionary
As seen, two dictionaries named dict1
and dict2
have been defined. In the following examples, we will append them to my_list
. With no further ado, let’s skip to the examples!
Example 1: Append Single Dictionary to List using append()
In Example 1, I will show how to add a single dictionary to a list using the append() method. But before appending, first, we need to copy the dictionary to ensure that the later changes in the dictionary’s content will not impact the dictionary appended to the list. You can ignore this step if you don’t need independent objects of lists and dictionaries.
my_list.append(dict1.copy()) # Append a copy of the dictionary to the list print(my_list) # Print the list # [{'key1': 'value1', 'key2': 'value2'}]
As shown, dict1
has been successfully added to my_list
. What if we were interested in inserting multiple dictionaries? The answer is in the next example!
Example 2: Append Multiple Dictionaries to List using extend()
Using the extend() method, we can easily extend a list with multiple dictionaries as shown below.
my_list.extend((dict1.copy(), dict2.copy())) # Append the copies of dictionaries to the list print(my_list) # Print the list # [{'key1': 'value1', 'key2': 'value2'}, {'key3': 'value3', 'key4': 'value4'}]
The copies of dict1
and dict2
have been appended to my_list
successfully. Is there an alternative to using extend()? Yes, see the next example!
Example 3: Append Multiple Dictionaries to List using Concatenation Operator
Alternatively, you can use the concatenation operator +
to include multiple dictionaries in a list.
my_list + [dict1.copy(), dict2.copy()] # Append the copies of dictionaries to the list using concatenation print(my_list) # Print the list # [{'key1': 'value1', 'key2': 'value2'}, {'key3': 'value3', 'key4': 'value4'}]
First, the copies of dict1
and dict2
have been attached to a list using square brackets. Then the operation of addition took place. Finally, my_list
matching the previous example has been obtained. Well done!
At this point, I guess we are familiar with the copy-append method of adding dictionaries to lists. It is easy and intuitive. However, it is a bit different story when it comes to complex dictionaries containing structures like lists, dictionaries, etc. In such a case, we need a deep copy to copy the elements of dictionaries as well. Let’s see it in an example!
Example 4: Append Multiple Complex Dictionaries to List using extend()
In Example 4, first, I will import the deepcopy() function of the copy module to use in the implementation. Then I’ll create complex dictionaries containing lists and append the deep copies to the list using the extend() method.
from copy import deepcopy # Initialize an empty list dict1_new = {"key1": "value1", "key2": ["item1", "item2"]} # Create a dictionary containing lists dict2_new = {"key3": "value3", "key4": ["item3", "item4"]} # Create a sec dictionary containing lists my_list.extend((deepcopy(dict1_new), deepcopy(dict2_new))) # Append the deep copies of dictionaries to the list print(my_list) # Print the list # [{'key1': 'value1', 'key2': ['item1', 'item2']}, {'key3': 'value3', 'key4': ['item3', 'item4']}]
As shown, the copies of dict1_new
and dict2_new
have been attached to my_list
Hence, any changes in dict1_new
or dict2_new
including the elements in the lists: item1
, item2
, item3
and item4
will not impact the dictionaries appended to my_list
. Sounds good!
Video & Further Resources
Do you want to learn more about the extension of lists by dictionaries? Then you could have a look at the following video that I have published on my YouTube channel. I illustrate the Python programming syntax of this article in the video.
The YouTube video will be added soon.
Furthermore, you might want to read the related articles on www.statisticsglobe.com:
- How to Use Lists in Python
- Check if List of Lists is Empty in Python
- Difference Between List & Dictionary in Python
- Count Number of Dictionaries in List in Python
- Introduction to Python Programming
Summary: At this point, you should have learned how to include dictionaries in a list in the Python programming language. If you have further questions, let me know in the comments.
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 of her other tasks on Statistics Globe.
Statistics Globe Newsletter