Home » Programming » Python » Python Add Item To Dictionary

How to Add/Append Items to a Dictionary in Python [Examples]

This short tutorial will show you how to add single or multiple items (as key:value pairs) to a dictionary in the Python programming language.

What is a Dictionary in Python?

We’ve covered what dictionaries are in our article here.

There are a couple of ways to add items to a dictionary, so we’ve put together this article separately to make sure we’ve got everything covered!

Checking Whether a Key Exists in a Dictionary

As outlined in the article linked above, dictionaries store data as a series of key:value pairs.

Before adding new items to a dictionary, you may want to check whether a value already exists at the given key.

If a key already exists in a dictionary, assigning it a value with any of the below methods will overwrite the existing value.

The in keyword can be used to check whether a key already exists. This will return TRUE if the key exists and FALSE if it does not, so it can be used in an if statement:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

if 'colour' in myDictionary:
    print('colour exists in dictionary')

Adding a Single Item to a Dictionary

To add a new item to a dictionary, simply assign a value to the new key:

myDictionary = {
        "name": "Fred",
        "animal": "cat",
        "colour": "red",
        "age": 3
    } 

myDictionary["mood"] = "hungry" # Square brackets are used to set the value at a key, or add a new value if it does not exist

print(myDictionary)

Above, a new key:value pair is added to the dictionary for mood, and the change is confirmed by printing the result – which outputs:

{
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3,
    "mood": "hungry"
}

 

Adding Multiple Items to a Dictionary with the update() Method

Multiple values can be added or updated in a dictionary using the update() method.

Note that if you’re only adding or modifying a single value, the above method results in better performance.

Here’s how the update() method is used:

myDictionary = {
        "name": "Fred",
        "animal": "cat",
        "colour": "red",
        "age": 3
    } 

myDictionary.update({"mood":"sleepy", "fluffiness": "so fluffy"})

print(myDictionary)

After a dictionary is defined, call the update() method from it, passing a new dictionary with the values you wish to have added. The above code results in myDictionary having two values appended to it:

{
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3,
    "mood": "sleepy",
    "fluffiness": "so fluffy"
}

Adding Values to Nested Dictionaries

The above methods for appending values to a dictionary work just as well with dictionaries nested within other dictionaries:

myDictionary = {
        "name": "Fred",
        "animal": "cat",
        "colour": "red",
        "age": 3,
        "collar": {
            "colour": "blue"
            }
    } 

myDictionary["collar"]["style"] = "studded"

myDictionary["collar"].update({"length": 20, "width": 3})

print(myDictionary)

The above code demonstrates appending to a nested dictionary, resulting in the following:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3,
    "collar": {
        "colour": "blue",
        "style": "studded",
        "length": 20, 
        "width": 3
        }
}

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment