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 OverflowW3Schools
w3schools.com โบ python โบ python_dictionaries_add.asp
Python - Add Dictionary Items
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
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.
Videos
05:18
Python interview question #19: Add to a dict - YouTube
03:07
Python Dictionary - add and update items from a dictionary - YouTube
01:19
How to Add elements to an already created Python Dictionary | Amit ...
05:26
How to Append Item to Dictionary in Python | Update Dictionary ...
01:25
โ
How To Append To A Dictionary In Python ๐ด - YouTube
07:13
Changing and Adding Dictionary Items in Python - YouTube
Top answer 1 of 16
4459
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))
DigitalOcean
digitalocean.com โบ community โบ tutorials โบ python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - 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.
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.
W3Schools
w3schools.com โบ python โบ python_ref_dictionary.asp
Python Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries. Learn more about dictionaries in our Python Dictionaries Tutorial.
GeeksforGeeks
geeksforgeeks.org โบ python โบ python-dictionary
Python Dictionary - GeeksforGeeks
d = {1: 'Geeks', 2: 'For', 3: 'Geeks'} # Adding a new key-value pair d["age"] = 22 # Updating an existing value d[1] = "Python dict" print(d) Output ยท {1: 'Python dict', 2: 'For', 3: 'Geeks', 'age': 22} Dictionary items can be removed using built-in deletion methods that work on keys: del: removes an item using its key ยท
Published ย 1 week ago
GeeksforGeeks
geeksforgeeks.org โบ python โบ python-add-items-to-dictionary
Python - Add Items to Dictionary - GeeksforGeeks
July 23, 2025 - It adds the new items to the dictionary or updates the values of existing keys. setdefault() method adds an item to the dictionary only if the key does not already exist.
W3Schools
w3schools.com โบ python โบ gloss_python_dictionary_add_item.asp
Python Adding Items in a Dictionary
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
SitePoint
sitepoint.com โบ python hub โบ adding items to dictionaries
Python - Adding Items to Dictionaries | SitePoint โ SitePoint
It follows the same pattern as other Python operators like += for numbers. Just as number += 5 adds 5 to your number, capitals |= new_capitals adds new items to your dictionary. One thing to note: just like with other methods, if there are any matching keys, the new values will replace the old ones:
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.
W3Schools
w3schools.com โบ python โบ python_dictionaries.asp
Python Dictionaries
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
Tutorialspoint
tutorialspoint.com โบ python โบ python_add_dictionary_items.htm
Python - Add Dictionary Items
It is part of the collections module in Python's standard library. We can add dictionary items using the collections.defaultdict() method by specifying a default factory, which determines the default value for keys that have not been set yet.
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 - The keys in the dictionary are Name, Address and Age. Usingappend() methodwe canupdate the values for the keys in the dictionary.
freeCodeCamp
freecodecamp.org โบ news โบ add-to-dict-in-python
Adding to Dict in Python โ How to Append to a Dictionary
February 28, 2023 - 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 with an iterable of key-value pairs, such as a list of tuples. Let's see this in action. myDict = {'a': 1, 'b': 2} new_data = [('c', 3), ('d', 4)] myDict.update(new_data) print(myDict) ... In Python, a dictionary can be updated or made from scratch using the dict() constructor...
GeeksforGeeks
geeksforgeeks.org โบ python โบ append-a-value-to-a-dictionary-python
Append a Value to a Dictionary Python - GeeksforGeeks
July 23, 2025 - Explanation: update() adds key-value pairs from another dictionary to the existing dictionary d. If a key already exists, its value is updated otherwise, the key-value pair is added.