How can I add new keys to Python dictionary?

Better Stack Team
Updated on January 26, 2023

You can add a new key-value pair to a dictionary in Python by using the square brackets [] to access the key you want to add and then assigning it a value using the assignment operator =. For example:

my_dict = {'a': 1, 'b': 2}
my_dict['c'] = 3

This would output:

{'a': 1, 'b': 2, 'c': 3}

You can also use the update() method to add multiple key-value pairs to a dictionary at once. For example:

my_dict = {'a': 1, 'b': 2}
my_dict.update({'c': 3, 'd': 4})

This would output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack