AppDividend
Latest Code Tutorials

Python Add To Dictionary Example | Add Key In Python Dict

0

Python add to dictionary example is today’s topic. Python has so many data structures, and one of them is the Dictionary. Python dictionary is one of the built-in Python data types. Add and remove elements are is the standard operations of data structures, and add element to dictionary is one of them. Python dictionary elements are key-value pairs.

Python add to dictionary

Perfect meaning of Python add to dictionary is either items or elements insert into dictionary. We will see different ways of inserting key-value pairs in Python dict.

Some of the data structures have specific methods to add elements in the structure, but there is no explicitly defined method to add the new key to the dictionary.

If you want to add the new key to a dictionary, then you can use the assignment operator with the dictionary key.

See the following snippet.

dict[key] = value

Now, one thing to keep in mind that if the key already exists, then it will override the value.

Example of how to add element in Python dictionary

See the following code.

# app.py

disneyplus = {
    'name': 'Mandalorian',
    'mvp': 'Baby Yoda'
}
print('Before add', disneyplus)
disneyplus['season'] = 1
print('After add', disneyplus)

Output

➜  pyt python3 app.py
Before add {'name': 'Mandalorian', 'mvp': 'Baby Yoda'}
After add {'name': 'Mandalorian', 'mvp': 'Baby Yoda', 'season': 1}
➜  pyt

Python override dictionary values

Let’s take a scenario in which we override the existing dictionary values.

# app.py

disneyplus = {
    'name': 'Mandalorian',
    'mvp': 'Baby Yoda'
}
print('Before Override', disneyplus)
disneyplus['mvp'] = 'Cara Dune'
print('After Override', disneyplus)

Output

➜  pyt python3 app.py
Before Override {'name': 'Mandalorian', 'mvp': 'Baby Yoda'}
After Override {'name': 'Mandalorian', 'mvp': 'Cara Dune'}
➜  pyt

In the above code, we are overriding the value of key: ‘mvp’ from ‘Baby Yoda’ to ‘Cara Dune’.

Now, what if we want to add a key only if it’s not present in the dictionary.

Well, we can achieve this using if condition, not in operator, and dict.keys() method.

# app.py

disneyplus = {
    'name': 'Mandalorian',
    'mvp': 'Baby Yoda'
}
if 'mvp' not in disneyplus.keys():
  disneyplus['mvp'] = 'Cara Dune'
print('After if condition', disneyplus)

Output

➜  pyt python3 app.py
After if condition {'name': 'Mandalorian', 'mvp': 'Baby Yoda'}
➜  pyt

In the above code, we are checking the condition that if the mvp property is there, then we don’t override that value; otherwise, we will override it.

How to update Python dictionary

Overriding the existing dictionary value is the same as updating Python dictionary values.

We can use that same method to update Python dictionary values as well.

See the following code.

# app.py

disneyplus = {
    'name': 'Mandalorian',
    'mvp': 'Baby Yoda'
}

print('Before Update', disneyplus)
disneyplus['mvp'] = 'Cara Dune'
print('After Update', disneyplus)

Output

➜  pyt python3 app.py
Before Update {'name': 'Mandalorian', 'mvp': 'Baby Yoda'}
After Update {'name': 'Mandalorian', 'mvp': 'Cara Dune'}
➜  pyt

You can see from the output that the mvp value is updated.

Python __setitem__ method to add key-value pair to a dict

We can use the __setitem__ method to add a key-value pair to a dictionary.

But, the Python  __setitem__ method should be avoided because of its computationally poor performance.

Let’s see the example.

# app.py

disneyplus = {
    'name': 'Mandalorian',
    'mvp': 'Baby Yoda'
}

print('Before Add', disneyplus)
disneyplus.__setitem__('season', 1)
print('After Add', disneyplus)

Output

➜  pyt python3 app.py
Before Add {'name': 'Mandalorian', 'mvp': 'Baby Yoda'}
After Add {'name': 'Mandalorian', 'mvp': 'Baby Yoda', 'season': 1}
➜  pyt

Finally, How to add key in Python Dict example is over. Thanks for taking it.

Related Posts

Python dictionary fromKeys()

Python dictionary get()

Python dictionary to dataframe

Python dictionary to json

Python dictionary to string

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.