Skip to content

How to Append a Dictionary to a List in Python

How to Append a Dictionary to a List in Python Cover Image

Python lists are mutable objects that can contain different data types. Because of this, you’ll often find yourself appending items to lists. In this tutorial, you’ll learn how to append a dictionary to a list in Python.

While this may seem like a trivial task, there is a little complexity to it. Don’t worry, though! By the end of this tutorial, you’ll understand why some unexpected behavior happens and how to resolve it.

By the end of reading this tutorial, you’ll have learned the following:

  • How to append a dictionary to a list in Python
  • Why you need to append a copy of a dictionary
  • What happens if you don’t append a copy of a dictionary

How to Append a Dictionary to a List in Python

Python provides a number of different ways to append values to a list. In fact, you can use the .append() method to add a dictionary to a list. So, you may be wondering why you’re reading an entire article about this. Chances are, you tried the .append() method and ran into some unexpected results. Let’s dive into how to add a dictionary to a list in Python.

Let’s take a look at the .append() method itself before diving further into appending dictionaries:

# Understanding the Python list.append() Method
list.append(x)

The list.append() method accepts an object to append to a given list. Because the method works in place, there is no need to re-assign the list to another variable. As the name suggests, the .append() method will add the item to the end of a given list.

How to Append a Dictionary to a List in Python

  1. Declare both your list and dictionary

    We’ll use two variables to create our list and dictionary in this case. Our list will be called values and our dictionary will be called ages.

  2. Append the dictionary after applying the copy() method to it

    The Python list.append() method works in place, meaning that you don’t need to re-assign it. Rather than passing in the dictionary, apply the .copy() method to the code.

  3. Python will append the dictionary using its values, rather than a reference

    By using the .copy() method, we append the dictionary’s values, rather than their reference points. Without this, the dictionary in the list would be linked to the original.

Let’s take a look at an example to see how this works:

# Appending a Dictionary to a List in Python
values = ['datagy', '.io']
ages = {'Nik': 33}

values.append(ages.copy())
print(values)

# Returns:
# ['datagy', '.io', {'Nik': 33}]

In the code block above, we passed the copy of the ages dictionary to the .append() method. This, in many ways, decouples the values from the reference and appends only the details of the dictionary.

Understanding Issues When Appending a Dictionary to a Python List

If you’ve read this far, you may be wondering why we needed to use the .copy() method when appending our dictionary. Let’s take a look at an example of what happens when we don’t do this:

# Appending a Dictionary Without Copying
values = ['datagy', '.io']
ages = {'Nik': 33}

values.append(ages)

print('List before modifying: ', values)
ages['Nik'] = 34
print('List after modifying: ', values)

# Returns:
# List before modifying:  ['datagy', '.io', {'Nik': 33}]
# List after modifying:  ['datagy', '.io', {'Nik': 34}]

Let’s break down what we’re doing in the code block above:

  1. We declare our dictionary and list, as before
  2. We then append the dictionary to the list without copying it
  3. Then, we print the list
  4. We modify one of the values in the dictionary by direct assignment
  5. Then, we print the values again

We can see that this yielded some unexpected results! We updated the dictionary outside of the list, but the dictionary inside the list updated anyway!

This is the reason why we needed to create a copy of the dictionary prior to appending it. When we simply append the dictionary by itself, it appends the reference of the object to the list. The dictionary, otherwise, is actually just a reference itself. This means that it shares the same location in memory and if one is updated, the other gets updated too.

By appending the copy, the reference is broken! This ensures that when we update one of the dictionaries, the other isn’t updated as well. In fact, the two dictionaries then no longer share the same location in memory, making the operation much safer!

Conclusion

In this tutorial, you learned how to append a dictionary to a list in Python. While it may seem like a trivial task, there is some complexity to it due to the nature of how Python tries to save memory.

You learned how to append a dictionary to a list by using the .append() method and the .copy() method. This ensured that the dictionary’s values were appended, rather than the reference to the dictionary. From there, you learned why this process was necessary, by walking through an example of what happens when you don’t first copy the dictionary.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

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