You create a new key/value pair on a dictionary by assigning a value to that key

d = {'key': 'value'}
print(d)  # {'key': 'value'}

d['mynewkey'] = 'mynewvalue'

print(d)  # {'key': 'value', 'mynewkey': 'mynewvalue'}

If the key doesn't exist, it's added and points to that value. If it exists, the current value it points to is overwritten.

Answer from Paolo Bergantino on Stack Overflow
Top answer
1 of 16
4462

You create a new key/value pair on a dictionary by assigning a value to that key

d = {'key': 'value'}
print(d)  # {'key': 'value'}

d['mynewkey'] = 'mynewvalue'

print(d)  # {'key': 'value', 'mynewkey': 'mynewvalue'}

If the key doesn't exist, it's added and points to that value. If it exists, the current value it points to is overwritten.

2 of 16
1365

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1  # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4})  # Updates 'c' and adds 'd'

Python 3.9+:

The update operator |= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data)  # Modifies data3, not data
data3.update(data2)  # Modifies data3, not data2

Python 3.5+:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

Python 3.9+:

The merge operator | now works for dictionaries:

data = data1 | {'c':3,'d':4}

Deleting items in dictionary

del data[key]  # Removes specific element in a dictionary
data.pop(key)  # Removes the key & returns the value
data.clear()  # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-new-keys-to-a-dictionary
Add new keys to a dictionary in Python - GeeksforGeeks
July 11, 2025 - d = {"a": 1, "b": 2} # Adding a ... exists then its value is updated. We can use | operator to create a new dictionary by merging existing dictionaries or adding new keys and values....
Discussions

How do you add to a value in a dictionary?
new_dict[word] += 1 This presumes it already has a value, and as written, will be the case. More on reddit.com
🌐 r/learnpython
13
1
October 14, 2021
way to add a key and append new value to dictionary of lists?
This is exactly what defaultdict is for. from collections import defaultdict my_dict = defaultdict(list) my_dict['a'].append(2) my_dict['a'].append(3) my_dict['w'].extend([7, 5]) More on reddit.com
🌐 r/learnpython
4
1
May 13, 2022
How to insert dict key after specific key
How can I insert key with value after specific key in a dictionary Example: {“key1”: 5, “key2”: 10, “key3”: 15, “key4”: 20} I want to insert after “key2” “key5”: 25 Final result something like this {“key1”: 5, “key2”: 10, “key5”: 25, “key3”: 15, “key4”: ... More on discuss.python.org
🌐 discuss.python.org
1
0
May 15, 2020
How to add values to an already existing key in dictionary
Hi! I’m trying to add new values to an already existing key in my dictionary. Let’s suppose I have the following dictionary: my_dict { ‘key_1’: ‘value_1’, ‘key_2: ‘value_2’ } My goal, in this example, is to add the values on this list: [‘value_3’, ‘value_4’] either ... More on forum.uipath.com
🌐 forum.uipath.com
8
1
March 6, 2021
🌐
Codecademy
codecademy.com › article › python-dictionary-append-how-to-add-items-to-dictionary
Python Dictionary Append: How to Add Items to Dictionary | Codecademy
The most direct and commonly used way to add or update items in a Python dictionary is by using square brackets ([]) with the assignment operator (=). This method allows you to assign a value to a new key or update the value of an existing key.
🌐
W3Schools
w3schools.com › python › python_dictionaries_add.asp
Python - Add Dictionary Items
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"] = "red" print(thisdict) Try it Yourself » · 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. Add a color item to the dictionary by using the update() method:
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.5rc1 documentation
When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments: >>> dict(sape=4139, guido=4127, jack=4098) {'sape': 4139, 'guido': 4127, 'jack': 4098} When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.
🌐
Reddit
reddit.com › r/learnpython › how do you add to a value in a dictionary?
r/learnpython on Reddit: How do you add to a value in a dictionary?
October 14, 2021 -

I know how to update values in a dictionary. That's easy, and there are multiple ways to do it. However, what I'm looking for is how to add to a value in a dictionary. For example:

    def frequency_dictionary(words):
      new_dict = {}
      for word in words:
        if word not in new_dict:
          new_dict[word] = 1
        else:
           #?????
      return new_dict

I want this function to add 1 to the value of a word for each time it is featured in the words list. For example, print(frequency_dictionary(["apple", "apple", "cat", 1])) should return {"apple":2, "cat":1, 1:1}. The comment in the function with question marks is the part I am stuck on. What are some options to do this?

I tried looking this up, but all I got was info on how to update values, not add to them.

Find elsewhere
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › python: how to add items to dictionary
Python: How to Add Items to Dictionary
December 19, 2025 - The __setitem__ method is another way to add an item to a dictionary. The syntax is: ... The method sets the item key as "three" with the value 3.
🌐
Analytics Vidhya
analyticsvidhya.com › home › 5 methods to add new keys to a dictionary in python
5 Methods to Add New Keys to a Dictionary in Python
February 7, 2025 - Understand the differences between the available methods for adding keys to dictionaries and their appropriate use cases. Explore practical examples demonstrating adding and manipulating key-value pairs in Python dictionaries.
🌐
Reddit
reddit.com › r/learnpython › way to add a key and append new value to dictionary of lists?
r/learnpython on Reddit: way to add a key and append new value to dictionary of lists?
May 13, 2022 -

I have this dictionary:

dict = {'a':[], 'b':[]}

and im appending the lists like so:

dict['a'] += [2]

returns: {'a':[2], 'b':[]}

unfortunately im constantly adding new keys to this dictionary as my project progresses and id like to just be able to add a new key and update it in the same line. If the key already exists, then just append to the existing key and don't duplicate it.

dict = {'a':[], 'b':[]} <- this is what i want to avoid having to make,

this is what id prefer:

dict = {}

dict.addkey['a'] += [2]

dict.addkey['a'] += [3]

dict.addkey['w'] += [7, 5]

print(dict)

returns: {'a': [2, 3], 'w': [7, 5]}

🌐
DataCamp
datacamp.com › tutorial › python-dictionary-append
Python Dictionary Append: How to Add Key-Value Pairs | DataCamp
August 6, 2024 - Appending elements to a dictionary ... The most straightforward way to add a single key-value pair to a dictionary is using square bracket notation....
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - However, dictionary keys are immutable and need to be unique within each dictionary. This makes dictionaries highly efficient for lookups, insertions, and deletions. Dictionaries are widely used in Python for various applications such as counting occurrences, grouping data, and storing configurations. Despite their versatility, there’s no built-in add method for dictionaries.
🌐
freeCodeCamp
freecodecamp.org › news › add-to-dict-in-python
Adding to Dict in Python – How to Append to a Dictionary
September 1, 2024 - The above code will create a dictionary myDict with two key-value pairs. Then we added a new key-value pair 'c' : 3 to the dictionary by just assigning the value 3 to the key 'c'. After the code is executed, the dictionary myDict will now contain the key-value pair 'c': 3.
🌐
Python.org
discuss.python.org › python help
How to insert dict key after specific key - Python Help - Discussions on Python.org
May 15, 2020 - How can I insert key with value ... Example: {“key1”: 5, “key2”: 10, “key3”: 15, “key4”: 20} I want to insert after “key2” “key5”: 25 Final result something like this {“key1”: 5, “key2”: 10, “key5”: 25, “key3”: 15, “key4”: ......
🌐
Python Forum
python-forum.io › thread-31044.html
Adding keys and values to a dictionary
Hey Guys. Trying to add value, key in this order to a dictionary. Checking with the debugger it shows that index 0 is added to new_dict, and then in the second iteration, index 1 replaces index 0 and then index 2 is added correctly. Any idea why? ...
🌐
UiPath Community
forum.uipath.com › help › activities
How to add values to an already existing key in dictionary - Activities - UiPath Community Forum
March 6, 2021 - Hi! I’m trying to add new values to an already existing key in my dictionary. Let’s suppose I have the following dictionary: my_dict { ‘key_1’: ‘value_1’, ‘key_2: ‘value_2’ } My goal, in this example, is to add the values on this list: [‘value_3’, ‘value_4’] either ...
🌐
Stack Abuse
stackabuse.com › python-how-to-add-keys-to-dictionary
Python: How to Add Keys to a Dictionary
March 8, 2023 - The quickest way to add a single item to a dictionary is by using a dictionary's index with a new key and assigning a value. For example, we add a new key-value pair like this: ... Python allows adding multiple items to dictionaries as well.
🌐
W3docs
w3docs.com › python
How can I add new keys to a dictionary? | W3Docs
To add a new key-value pair to a dictionary in Python, you can use the update() method or simply assign a value to a new key using square brackets [].
🌐
W3Schools
w3schools.com › python › gloss_python_dictionary_add_item.asp
Python Adding Items in a Dictionary
Python Dictionaries Tutorial Dictionary Access Dictionary Items Change Dictionary Item Loop Dictionary Items Check if Dictionary Item Exists Dictionary Length Remove Dictionary Items Copy Dictionary Nested Dictionaries ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
Quora
quora.com › How-do-you-add-to-a-dictionary-in-Python
How to add to a dictionary in Python - Quora
Answer (1 of 16): To add a value you need a key (since a dictionary stores a key value pair). With the key then adding a value is easy : [code]the_dict[key] = value [/code]This will add a key to a dictionary if the key doesn’t exist, and replace the value being stored for that key if the ...