Skip to content

Python: Add Key:Value Pair to Dictionary

How to Add Items to a Python Dictionary Cover Image

In this tutorial, you’ll learn how to add key:value pairs to Python dictionaries. You’ll learn how to do this by adding completely new items to a dictionary, adding values to existing keys, and dictionary items in a for loop, and using the zip() function to add items from multiple lists.

What are Python dictionaries? Python dictionaries are incredibly helpful data types, that represent a hash table, allowing data retrieval to be fast and efficient. They consist of key:value pairs, allowing you to search for a key and return its value. Python dictionaries are created using curly braces, {}. Their keys are required to be immutable and unique, while their values can be any data type (including other dictionaries) and do not have to be unique.

The Quick Answer: Use dict[key] = [value] to Add Items to a Python Dictionary

Quick Answer - Python Add Append Item to Dictionary
How to Add an Item to a Python Dictionary

Add an Item to a Python Dictionary

The easiest way to add an item to a Python dictionary is simply to assign a value to a new key. Python dictionaries don’t have a method by which to append a new key:value pair. Because of this, direct assignment is the primary way of adding new items to a dictionary.

Let’s take a look at how we can add an item to a dictionary:

# Add an Item to a Python Dictionary
dictionary = {'Nik': 32, 'Kate': 32, 'Jane': 41, 'Doug': 23}

dictionary['Evan'] = 30
print(dictionary)

# Returns: {'Nik': 32, 'Kate': 32, 'Jane': 41, 'Doug': 23, 'Evan': 30}

We can see here that we were able to easily append a new key:value pair to a dictionary by directly assigning a value to a key that didn’t yet exist.

What can we set as dictionary keys? It’s important to note that we can add quite a few different items as Python dictionary keys. For example, we could make our keys strings, integers, tuples – any immutable item that doesn’t already exist. We can’t, however, use mutable items (such as lists) to our dictionary keys.

In the next section, you’ll learn how to use direct assignment to update an item in a Python dictionary.

Update an Item in a Python Dictionary

Python dictionaries require their keys to be unique. Because of this, when we try to add a key:value pair to a dictionary where the key already exists, Python updates the dictionary. This may not be immediately clear, especially since Python doesn’t throw an error.

Let’s see what this looks like, when we add a key:value pair to a dictionary where the key already exists:

# Update an Item in a Python Dictionary
dictionary = {'Nik': 32, 'Kate': 32, 'Jane': 41, 'Doug': 23}

dictionary['Nik'] = 30
print(dictionary)

# Returns: {'Nik': 30, 'Kate': 32, 'Jane': 41, 'Doug': 23}

We can see that when we try to add an item to a dictionary when the item’s key already exists, that the dictionary simply updates. This is because Python dictionaries require the items to be unique, meaning that it can only exist once.

In the next section, you’ll learn how to use a for loop to add multiple items to a Python dictionary.

Append Multiple Items to a Python Dictionary with a For Loop

There may be times that you want to turn two lists into a Python dictionary. This can be helpful when you get data from different sources and want to combine lists into a dictionary data structure.

Let’s see how we can loop over two lists to create a dictionary:

# Loop Over Two Lists to Create a Dictionary
keys = ['Nik', 'Kate', 'Jane']
values = [32, 31, 30]
dictionary = {}

for i in range(len(keys)):
    dictionary[keys[i]] = values[i]

print(dictionary)

# Returns: {'Nik': 32, 'Kate': 31, 'Jane': 30}

Here, we loop over each value from 0 through to the length of the list minus 1, to access the indices of our lists. We then access the ith index of each list and assign them to the keys and values of our lists.

There is actually a much simpler way to do this – using the Python zip function, which you’ll learn about in the next section.

Add Multiple Items to a Python Dictionary with Zip

The Python zip() function allows us to iterate over two iterables sequentially. This saves us having to use an awkward range() function to access the indices of items in the lists.

Let’s see what this looks like in Python:

# Loop Over Two Lists to Create a Dictionary using Zip
keys = ['Nik', 'Kate', 'Jane']
values = [32, 31, 30]
dictionary = {}

for key, value in zip(keys, values):
    dictionary[key] = value

print(dictionary)

# Returns: {'Nik': 32, 'Kate': 31, 'Jane': 30}

The benefit of this approach is that it is much more readable. Python indices can be a difficult thing for beginner Python developers to get used to. The zip function allows us to easily interpret what is going on with our code. We can use the zip function to name our iterable elements, to more easily combine two lists into a dictionary.

If you’re working with a dictionary that already exists, Python will simply update the value of the existing key. Because Python lists can contain duplicate values, it’s important to understand this behaviour. This can often lead to unexpected results since the program doesn’t actually throw an error.

Conclusion

In this tutorial, you learned how to use Python to add items to a dictionary. You learned how to do this using direct assignment, which can be used to add new items or update existing items. You then also learned how to add multiple items to a dictionary using both for loops and the zip function.

To learn more about Python dictionaries, check out the official documentation here.

Additional Resources

To learn more about related topics, check out the articles 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 *