Python Add to Dictionary [Easy Step-By-Step]

Filed Under: Python
Python Add To Dictionary

Python dictionary is one of the built-in data types. Dictionary elements are key-value pairs. You can add to dictionary in Python using multiple methods. Let’s take a look at some of the different ways to append items to an existing Python dictionary.

Python add to Dictionary using “=” assignment operator

We do not have any specific Python way to update a dictionary. If you want to add a new key to the dictionary, then you can use the assignment operator with the dictionary key.

This is pretty much the same as assigning a new value to the dictionary. Since Python dictionaries are mutable, when you use the assignment operator, you’re simply adding new keys to the datastructure.

dict[key] = value

When a key already exists in the dictionary, the assignment operator will automatically update the values.

Let’s take a look at a demonstration below to see how this works out. If there is an existing key, the assignment operator will overwrite the values automatically.

d = {'a': 1, 'b': 2}
print(d)
d['a'] = 100  # existing key, so overwrite
d['c'] = 3  # new key, so add
d['d'] = 4
print(d)

Output:

{'a': 1, 'b': 2}
{'a': 100, 'b': 2, 'c': 3, 'd': 4}

Append values to a dictionary using the update() method

The Python dictionary offers an update() method that allows us to append a dictionary to another dictionary. The update() method automatically overwrites the values of any existing keys with the new ones. So make sure that you do not accidentally overwrite any valueable piece of information here.

Let’s see how we can use the dictionary update() method to add new values to our dictionary:

blog = {'Website':'Journaldev', 'tutorial':'Append to Python dictionary'}
print("Here are the current details: ", blog)

# Adding the author details to the dictionary
blog.update({'Author':'Pankaj Kumar'})
print("Updated dictionary is: ", blog)

# Appending another dictionary
guests = {'Guest1':'Meghna'}
blog.update(guests)
print("Updated dictionary is: ", blog)

Let’s take a look at how the update() method works to add new items to a Python dictionary:

Here are the current details:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary'}
Updated dictionary is:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary', 'Author': 'Pankaj Kumar'}
Updated dictionary is:  {'Website': 'Journaldev', 'tutorial': 'Append to Python dictionary', 'Author': 'Pankaj Kumar', 'Guest1': 'Meghna'}

Add items to Python dictionary without overwriting

Notice how both the methods automatically overwrite any existing value if the key is present. If you know that your program may end up having duplicate keys, it’s best to add a conditional append instead of directly appending the values.

You can do the same using an if condition here to ensure that the keys within the dictionary are not overwritten.

Here’s a simple example of how you can make this work. You can also choose to use the try catch exception however, using an if condition will be the easiest one to start with.

if 'c' not in d.keys():
    d['c'] = 300

if 'e' not in d.keys():
    d['e'] = 5

print(d)

Output:

{'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

Notice that ‘c’ value is not changed because of the if condition.

Conclusion

That’s all for adding keys to a dictionary in python.

Comments

  1. hamza says:

    how can add new column in tuple
    # List of tuples
    listofTuples = [(“Riti” , 11), (“Aadi” , 12), (“Sam” , 13),(“John” , 22),(“Lucy” , 90)]
    thank

    1. james says:

      you can add tuple to tuple like that

      for i in range(len(listofTuples)) :
      listofTuples [i] += (“new item”,)

  2. KMJ248 says:

    Thank you, this information was clearly laid out!

  3. RTFM says:

    You don`t need keys()
    d={c: alpha}
    if c in d:
    pass

  4. Niik says:

    What will happen if you don’t add the .keys()? will it also check if the values of keys are the value you want to input?

  5. confused says:

    thanks it really helped!
    have a question tho, can you make mathematical operations in the values to the keys?

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content