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
4464

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 › add-a-keyvalue-pair-to-dictionary-in-python
Add a key value pair to Dictionary in Python - GeeksforGeeks
This operation allows us to expand the dictionary by adding new entries or modify the value of an existing key. For example, starting with dictionary d = {'key1': 'geeks', 'key2': 'for'}, we add multiple key-value pairs at once: 'key3': 'Geeks', ...
Published   July 11, 2025
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
Append values to Dictionary
from collectioms import defaultdict d = defaultdict(list) [...] d[key].append(value) is probably what you want. You don't have to use defaultdict, you can also do sth like d = {} [...] if key in d: d[key].append(value) else: d[key] = [ value ] More on reddit.com
🌐 r/learnpython
9
2
June 8, 2021
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
🌐
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'}
🌐
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....
🌐
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....
🌐
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.
🌐
Guru99
guru99.com › home › python › python dictionary append: how to add key/value pair
Python Dictionary Append: How to Add Key/Value Pair
August 13, 2025 - To append an element to an existing dictionary, you have to use the dictionary name followed by square brackets with the key name and assign a value to it.
Find elsewhere
🌐
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....
🌐
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.

🌐
freeCodeCamp
freecodecamp.org › news › add-to-dict-in-python
Adding to Dict in Python – How to Append to a Dictionary
February 28, 2023 - By passing a dictionary containing the new key-value pair as an argument to the dict() constructor, we can add a single key-value pair to an existing dictionary.
🌐
W3Schools
w3schools.com › python › python_dictionaries_add.asp
Python - Add Dictionary Items
Python Examples Python Compiler ... = "red" print(thisdict) Try it Yourself » · The update() method will update the dictionary with the items from a given argument....
🌐
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 - You can also use the dictionary comprehension and dictionary constructor together to add or update the key-value pairs: prices = {**prices, **{"Pear": 2, "Grapefruit": 2, "Orange": 3}} print(prices) # will print {"Apple": 2, "Orange": 3, "Avocado": ...
🌐
datagy
datagy.io › home › python posts › python: add key:value pair to dictionary
Python: Add Key:Value Pair to Dictionary • datagy
December 19, 2022 - In this tutorial, you’ll learn how to add key:value pairs to Python dictionaries. You’ll learn how to do this by adding completely new items to a dictionary, adding values to existing keys, and dictionary items in a for loop, and using the zip() function to add items from multiple lists.
🌐
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 - This article will explore various methods to add new keys to a dictionary in Python and discuss some best practices to follow. ... Learn the fundamentals of key-value pairs in Python dictionaries and their role in efficiently storing and retrieving data.
🌐
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.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › python: how to add items to dictionary
Python: How to Add Items to Dictionary
April 1, 2026 - The assignment operator (=) sets a value to a dictionary key: ... The assignment operator adds a new key-value pair if the key does not exist.
🌐
Flexiple
flexiple.com › python › add-values-to-dictionary-python
How To Add Values To A Dictionary In Python - Flexiple
Assign Values Using Unique KeysMerging Two Dictionaries Using update()Add Values To Dictionary Using Two Lists Of The Same LengthConverting A List To The DictionaryAdd Values To Dictionary Using The merge( | ) OperatorAdd Values To Dictionary Using The in-place merge( |= ) Operator ... Python dictionaries are versatile and essential for managing key-value pairs in programming.
🌐
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 - This is the simplest approach, where you use the assignment operator to set a new key-value pair in the dictionary. The second script introduces the update() method, which allows you to add multiple key-value pairs to a dictionary at once.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-add-value-to-existing-key
Python Dictionary Add Value to Existing Key - GeeksforGeeks
July 23, 2025 - Unlike adding new key-value pairs, this operation focuses on updating the value of an existing key, allowing us to increment, concatenate or otherwise adjust its value as needed. For example, consider a dictionary d = {'a': 1, 'b': 2}. If we want to add 3 to the value of key 'a', we can directly modify the value like this: d['a'] += 3, resulting in d = {'a': 4, 'b': 2}.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-dictionary-key
Python Add Dictionary Key - GeeksforGeeks
December 8, 2024 - The simplest way to add a key to a dictionary is through direct assignment. When you assign a value to a new key that doesn't already exist in the dictionary, Python will automatically add the key-value pair.