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
4463

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 - The update() method can be use to merge dictionaries or add multiple keys and their values in one operation. ... d = {"a": 1, "b": 2} # Adding a single key-value pair d.update({"c": 3}) # Adding multiple key-value pairs d.update({"d": 4, "e": ...
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 force Python to use Double quotes and not apostrophes for strings?

The single quotes are just there to show you that it's a string. They are not actually part of the string.

I can't think of an easy way to change that. You would have to make your own str class, I think.

More on reddit.com
๐ŸŒ r/learnpython
8
0
November 19, 2014
How to update a nested dict with a nested dict?

I don't understand what you're trying to do. Can you show an example of your expected outcome?

More on reddit.com
๐ŸŒ r/learnpython
7
6
April 19, 2019
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-dictionary-append
Python Dictionary Append: How to Add Key-Value Pairs | DataCamp
August 6, 2024 - In this example, we start with dictionary_w_list where the key 'fruits' maps to a list ['apple', 'banana']. By using the .append() method, we add 'cherry' to the list. This technique is particularly useful when managing collections of items within a single dictionary. Python 3.9 introduced the merge operator (|) for combining dictionaries.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - The following example demonstrates how to to create two dictionaries and use the merge operator to create a new dictionary that contains the key-value pairs from both: site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'} guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ add-a-keyvalue-pair-to-dictionary-in-python
Add a key value pair to Dictionary in Python - GeeksforGeeks
For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'for'}, we add multiple key-value pairs at once: 'key3': 'Geeks', 'key4': 'is', 'key5': 'portal', and 'key6': 'Computer'.
Published ย  July 11, 2025
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ add-new-keys-to-a-dictionary
Here is how to add new keys to a dictionary in Python
New: Practice Python, JavaScript & SQL with AI feedback โ€” Try ActiveSkill free โ†’ ร— ... my_dict = { "key1": "value1" } # Add a new key-value pair to the dictionary my_dict["key2"] = "value2"
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ add-to-dict-in-python
Adding to Dict in Python โ€“ How to Append to a Dictionary
September 1, 2024 - Example using the update() method to add multiple entries to a dictionary: myDict = {'a': 1, 'b': 2} new_data = {'c': 3, 'd': 4} myDict.update(new_data) print(myDict) ... Another fun thing about this method is that, we can use the update() method ...
Find elsewhere
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ how to add new keys to python dictionaries
How to Add New Keys to Python Dictionaries | Towards Data Science
December 16, 2024 - The simplest way to add a key-value pair in an existing dictionary, is through the value assignment to the (new) desired key: ... Note that if the key already exists, the above operation will replace the old value with the newly specified value.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python add keys to dictionary
Python Add keys to Dictionary - Spark By {Examples}
May 31, 2024 - Python provides several ways to add new keys to a dictionary. Dictionaries are data type in python, that allows you to store key-value pairs. To add keys
๐ŸŒ
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 - We learned about the bracket notation, update() method, setdefault() method, fromkeys() method, and the dict() constructor. We also discussed some best practices to follow while adding new keys to a dictionary. Following these methods and best practices, you can efficiently add new keys to dictionaries and manipulate data effectively in your Python programs.
๐ŸŒ
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.

๐ŸŒ
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.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-how-to-add-keys-to-dictionary
Python: How to Add Keys to a Dictionary
March 8, 2023 - Let's add new keys to a Python dictionary. We'll add a single key, and multiple keys with the update() function, merge operator | and update operator |=
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ python dictionary append: how to add key/value pair?
Python Dictionary Append: How To Add Key/Value Pair?
October 14, 2024 - Hereโ€™s how it works: 1. Merging ... iterating over dict2 and adding each key-value pair individually, you can simply use the update() method....
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ adding-new-keys-to-a-dictionary-in-python-a-simple-guide-82155a111bab
Adding New Keys to a Python Dictionary: An Easy Guide
August 24, 2024 - Dictionary comprehension provides a concise and readable way to create or merge dictionaries, improving code efficiency. ... Adding new keys to an existing Python dictionary is straightforward and can be done using multiple methods. Direct assignment is the simplest, while the update() method allows for bulk additions.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ dictionary โ€บ python-data-type-dictionary-exercise-2.php
Python: Add a key to a dictionary - w3resource
# Create a dictionary 'd' with two key-value pairs. d = {0: 10, 1: 20} # Print the original dictionary 'd'. print(d) # Update the dictionary 'd' by adding a new key-value pair {2: 30}. d.update({2: 30}) # Print the dictionary 'd' after the update, ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ add new keys to a dictionary in python
Add new keys to a dictionary in Python | Sentry
January 30, 2023 - The simplest way is to assign a ... syntax: prices = {"Apple": 1, "Orange": 2} # existing dictionary prices["Avocado"] = 3 # new key-value pair print(prices) # will print {"Apple": 1, "Orange": 2, "Avocado": 3} This same syntax ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dictionaries_add.asp
Python - Add Dictionary Items
Python Overview Python Built-in ... Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary ยท Built-in Modules Random Module Requests Module Statistics Module Math Module cMath Module ยท Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler ...
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python: add key:value pair to dictionary
Python: Add Key:Value Pair to Dictionary โ€ข datagy
December 19, 2022 - Letโ€™s see how we can loop over two lists to create a dictionary: # Loop Over Two Lists to Create a Dictionary keys = ['Nik', 'Kate', 'Jane'] values = [32, 31, 30] dictionary = {} for i in range(len(keys)): dictionary[keys[i]] = values[i] ...
๐ŸŒ
W3docs
w3docs.com โ€บ python
How can I add new keys to a dictionary? | W3Docs
# Create an empty dictionary my_dict = {} # Add a new key-value pair using the update() method my_dict.update({'name': 'John'}) # Add a new key-value pair using square brackets my_dict['age'] = 30 print(my_dict) # {'name': 'John', 'age': 30} ...