Add element to dictionary python

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.


Adding Items

Adding an item to the dictionary is done by using a new index key and assigning a value to it:

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
print(thisdict)

Try it Yourself »


Update Dictionary

The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added.

The argument must be a dictionary, or an iterable object with key:value pairs.

Example

Add a color item to the dictionary by using the update() method:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"color": "red"})

Try it Yourself »



Dictionaries are used to store data using the key-value structure in Python.

When you’re working with dictionaries, you may ask yourself, how can I add an item to a dictionary? This article will answer that question.

Add element to dictionary python

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

We’ll break down the basics of dictionaries, how they work, and how you can add an item to a Python dictionary. By the end of this tutorial, you’ll be an expert at adding items to Python dictionaries.

Python Dictionary: A Refresher

The dictionary data structure allows you to map keys to values. This is useful because keys act as a label for a value, which means that when you want to access a particular value, all you have to do is reference the key name for the value you want to retrieve.

Dictionaries generally store information that is related in some way, such as a record for a student at school, or a list of colors in which you can purchase a rug.

Consider the following dictionary:

scones = {
	"Fruit": 22,
	"Plain": 14,
	"Cinnamon": 4,
	"Cheese": 21
}

This dictionary contains four keys and four values. The keys are stored as a string, and appear to the left of the colon; the values are stored to the right of the colons; bound to a particular key in the dictionary.

Now, how do we add an item to this dictionary? Let’s break it down.

Python Add to Dictionary

To add an item to a Python dictionary, you should assign a value to a new index key in your dictionary.

Unlike lists and tuples, there is no add(), insert(), or append() method that you can use to add items to your data structure. Instead, you have to create a new index key, which will then be used to store the value you want to store in your dictionary.

Here is the syntax for adding a new value to a dictionary:

dictionary_name[key] = value

Let’s use an example to illustrate how to add an item to a dictionary. In our dictionary from earlier, we stored four keys and values. This dictionary reflected the stock levels of different flavors of scones sold at a cafe.

Suppose we have just baked a batch of 10 new cherry scones. Now, we want to add these scones to our dictionary of scone types. To do so, we can use this code:

scones = {
	"Fruit": 22,
	"Plain": 14,
	"Cinnamon": 4,
	"Cheese": 21
}

scones["Cherry"] = 10

print(scones)

Our code returns:

{'Fruit': 22, 'Plain': 14, 'Cinnamon': 4, 'Cheese': 21, 'Cherry': 10}

As you can see, our code has added the “Cherry” key to our dictionary, which has been assigned the value 10.

In our code, we first declared a dictionary called “scones” which stores information on how many scones are available at a cafe to order. Next, we added the key “Cherry” to our dictionary and assigned it the value 10 by using the following code:

Finally, we printed out the updated version of our dictionary.

Notice that, in the output of our code, our dictionary is not ordered. This is because data stored in a dictionary, unlike data stored in a list, does not have any particular order.

Tip: Adding and Updating Use the Same Notation

The same notation can be used to update a value in a dictionary. Suppose we have just baked a new batch of 10 cinnamon scones. We could update our dictionary to reflect this using the following line of code:

scones = {
	"Fruit": 22,
	"Plain": 14,
	"Cinnamon": 4,
	"Cheese": 21
}

scones["Cinnamon"] = 14

print(scones)

Our code returns:

{'Fruit': 22, 'Plain': 14, 'Cinnamon': 14, 'Cheese': 21}

This line of code sets the value associated with the “Cinnamon” key in our dictionary to 14.

Conclusion

There is no add(), append(), or insert() method you can use to add an item to a dictionary in Python. Instead, you add an item to a dictionary by inserting a new index key into the dictionary, then assigning it a particular value.

This tutorial discussed, with an example, how to add an item to a Python dictionary. Now you’re ready to start adding items to dictionaries like a Python expert!

How do I add elements to a dictionary in Python?

Let's see how can we add new keys to a dictionary using different ways to a dictionary..
Create a dictionary first..
Method 1: Add new keys using the Subscript notation..
Method 2: Add new keys using update() method..
Method 3: Add new keys using the __setitem__ method..
Method 4: Add new keys using the ** operator..

How do you add elements to a dictionary?

Method 1: Using += sign on a key with an empty value In this method, we will use the += operator to append a list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.