How do i add an item to a dictionary in python?

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 »


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.

Data structures help us organize and store collections of data. Python has built-in data structures like Lists, Sets, Tuples and Dictionaries.

Each of these structures have their own syntax and methods for interacting with the data stored.

In this article, we'll talk about Dictionaries, their features, and how to add items to them.

How to Create a Dictionary in Python

Dictionaries are made up of key and value pairs nested in curly brackets. Here's an example of a Dictionary:

devBio = {
  "name": "Ihechikara",
  "age": 120,
  "language": "JavaScript"
}
print[devBio]
# {'name': 'Ihechikara', 'age': 120, 'language': 'JavaScript'}

In the code above, we created a dictionary called devBio with information about a developer – the developer's age is quite overwhelming.

Each key in the dictionary – name, age and language – has a corresponding value. A comma separates each key and value pair from another. Omitting the comma throws an error your way.

Before we dive into how we can add items to our dictionaries, let's have a look at some of the features of a dictionary. This will help you easily distinguish them from other data structures in Python.

Features of a Dictionary

Here are some of the features of a dictionary in Python:

Duplicate Keys Are Not Allowed

If we create a dictionary that has two or multiple identical keys in it, the last key out of them will override the rest. Here's an example:

devBio = {
  "name": "Ihechikara",
  "name": "Vincent",
  "name": "Chikara",
  "age": 120,
  "language": "JavaScript"
}
print[devBio]
# {'name': 'Chikara', 'age': 120, 'language': 'JavaScript'}

We created three keys with an identical key name of name. When we printed our dictionary to the console, the last key having a value of "Chikara" overwrote the rest.

Let's see the next feature.

Items in a Dictionary Are Changeable

After assigning an item to a dictionary, you can change its value to something different.

devBio = {
  "name": "Ihechikara",
  "age": 120,
  "language": "JavaScript"
}

devBio["age"] = 1

print[devBio]

# {'name': 'Chikara', 'age': 120, 'language': 'JavaScript'}

In the example above, we reassigned a new value to age. This will override the initial value we assigned when the dictionary was created.

We can also use the update[] method to change the value of items in our dictionary. We can achieve the same result in the last example by using the update[] method – that is: devBio.update[{"age": 1}].

Items in a Dictionary Are Ordered

By being ordered, this means that the items in a dictionary maintain the order in which they were created or added. That order cannot change.

Prior to Python 3.7, dictionaries in Python were unordered.

In the next section, we will see how we can add items to a dictionary.

The syntax for adding items to a dictionary is the same as the syntax we used when updating an item. The only difference here is that the index key will include the name of the new key to be created and its corresponding value.

Here's what the syntax looks like: devBio[newKey] = newValue.

We can also use the update[] method to add new items to a dictionary. Here's what that would look like: devBio.update[{"newKey": newValue}].

Let's see some examples.

devBio = {
  "name": "Ihechikara",
  "age": 120,
  "language": "JavaScript"
}

devBio["role"] = "Developer"

print[devBio]

# {'name': 'Ihechikara', 'age': 120, 'language': 'JavaScript', 'role': 'Developer'}

Above, using the index key devBio["role"], we created a new key with the value of Developer.

In the next example, we will use the update[] method.

devBio = {
  "name": "Ihechikara",
  "age": 120,
  "language": "JavaScript"
}

devBio.update[{"role": "Developer"}]

print[devBio]

# {'name': 'Ihechikara', 'age': 120, 'language': 'JavaScript', 'role': 'Developer'}

Above, we achieved the same result as in the last example by passing in the new key and its value into the update[] method – that is: devBio.update[{"role": "Developer"}].

Conclusion

In this article, we learned what dictionaries are in Python, how to create them, and some of their features. We then saw two ways through which we can add items to our dictionaries.

Happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

How do you add an item to a dictionary?

Below are the two approaches which we can use..
Assigning a new key as subscript. We add a new element to the dictionary by using a new key as a subscript and assigning it a value. ... .
Using the update[] method. ... .
By merging two dictionaries..

How do you add a value to a dictionary in a list?

Appending a dictionary to a list with the same key and different values. Using append[] method. Using copy[] method to list using append[] method. Using deepcopy[] method to list using append[] method..
res_array is the resultabt array..
append is used to append to array..
tolist[] is used to convert a list..

How do you append an element to a key in a dictionary with Python?

How to append an element to a key in a dictionary with Python.
a_dict = collections. defaultdict[list].
a_dict["a"]. append["hello"].
print[a_dict].
a_dict["a"]. append["kite"].
print[a_dict].

Can you append values to a dictionary in Python?

We can add / append new key-value pairs to a dictionary using update[] function and [] operator. We can also append new values to existing values for a key or replace the values of existing keys using the same subscript operator and update[] function.

Chủ Đề