To append a key-value pair to a dictionary in Python, use square bracket notation (dict[key] = value). This method adds a new key with the specified value or updates the value if the key already exists.

For example:

my_dict = {'name': 'Alice', 'age': 25}
my_dict['city'] = 'New York'
print(my_dict)  # Output: {'name': 'Alice', 'age': 25, 'city': 'New York'}

Other common methods:

  • update(): Add multiple key-value pairs at once.

    my_dict.update({'email': 'alice@example.com', 'job': 'Engineer'})
  • setdefault(): Add a key only if it doesnโ€™t exist.

    my_dict.setdefault('country', 'USA')
  • Dictionary unpacking (**): Create a new dictionary with merged values (Python 3.5+).

    new_dict = {**my_dict, 'phone': '123-456-7890'}
  • Union operator (|): Merge dictionaries (Python 3.9+).

    merged = my_dict | {'salary': 50000}

Use square bracket notation for single additions, update() for multiple pairs, and setdefault() to avoid overwriting existing keys.

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
4456

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
1364

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))
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ add-to-dict-in-python
Adding to Dict in Python โ€“ How to Append to a Dictionary
February 28, 2023 - If there is a key 'c' in the dictionary already, the value would be updated to 3. Multiple key-value pairs can be simultaneously added to a dictionary using the update() method. This method inserts new entries into the original dictionary from ...
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ append values to dictionary
r/learnpython on Reddit: Append values to Dictionary
June 8, 2021 -

I am currently using a while loop to loop and get certain values. I then want to append these values to a dictionary. Every loop through I want to append to two keys

General structure of the code: https://pastebin.com/p4hJcKR5

I have tried using:

dict[key] = value

dict.append(value)

And neither have worked, dict.append gives an error and dict[key] just sets the dictionary to the most recent iteration instead of iterating for all values. Any help would be appreciated.

๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ add-a-keyvalue-pair-to-dictionary-in-python
Add a key value pair to Dictionary in Python - GeeksforGeeks
This is the simplest way to add or update a key-value pair in a dictionary. We access the dictionary by specifying the key inside square brackets and assign the corresponding value.
Published ย  July 11, 2025
๐ŸŒ
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 - Several methods are available in Python to add new keys to a dictionary. Letโ€™s explore each of these methods in detail. One of the simplest ways to add a new key-value pair to a dictionary is by using the bracket notation.
Find elsewhere
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ add new keys to a dictionary in python
Add new keys to a dictionary in Python | Sentry
The simplest way is to assign a value to a new key using Pythonโ€™s indexing/square brackets 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}
๐ŸŒ
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 - Python dictionary append is simply used to add a key/value to the existing dictionary. The dictionary objects are mutable.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ append item to dictionary in python
Append Item to Dictionary in Python - Spark By {Examples}
May 31, 2024 - The update() method allows iterable sequence of key/value pair as an argument and appends its key-value pairs to the original dictionary. The update() method updates the values of existing keys with the new values it keys are already present.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-add-new-keys-to-a-dictionary
Add new keys to a dictionary in Python - GeeksforGeeks
July 11, 2025 - Let's explore them with examples: The simplest way to add a new key is by using assignment operator (=). ... Explanation: d["c"]: creates a new key "c" and its value is assigned as "3".
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ add-a-key-value-pair-to-dictionary-in-python
Add a key value pair to dictionary in Python
We add a new element to the dictionary by using a new key as a subscript and assigning it a value. CountryCodeDict = {"India": 91, "UK" : 44 , "USA" : 1} print(CountryCodeDict) CountryCodeDict["Spain"]= 34 print "After adding" print(CountryCodeDict) Running the above code gives us the following ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - This will add a new key-value pair to the dictionary. If the key already exists, its value will be updated. ... You cannot directly use methods like .append() for dictionaries as they are used for lists. However, you can add a key-value pair using dictionary[key] = value.
๐ŸŒ
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.
๐ŸŒ
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 add element using append() to the dictionary, we have first to find the key to which we need to append to. ... The keys in the dictionary are Name, Address and Age. Usingappend() methodwe canupdate the values for the keys in the dictionary.
๐ŸŒ
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.
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ dictionary-append-python
Dictionary Append in Python | 3 Methods (with code)
September 11, 2023 - This method is particularly useful when you want to add multiple key-value pairs dynamically. Each of these methods provides flexibility in adding and updating data in dictionaries, and the choice of method depends on your specific use case and coding preferences. Now, let's print the 'student_info' dictionary to see the appended data. ... { 'name': 'Alice', 'age': 25, 'city': 'New York', 'gender': 'Female', 'major': 'Computer Science', 'grade': 'A', 'GPA': 3.8, 'courses': ['Python', 'Data Science'] }
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dictionaries_add.asp
Python - Add Dictionary Items
Python Examples Python Compiler ... โฎ Previous Next โฏ ยท Adding an item to the dictionary is done by using a new index key and assigning a value to it: thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } ...