You do not need to call d.keys(), so

if key not in d:
    d[key] = value

is enough. There is no clearer, more readable method.

You could update again with dict.get(), which would return an existing value if the key is already present:

d[key] = d.get(key, value)

but I strongly recommend against this; this is code golfing, hindering maintenance and readability.

Answer from Martijn Pieters on Stack Overflow
🌐
Reddit
reddit.com › r/python › append lists in dicts if key doesn't exist yet
r/Python on Reddit: Append lists in dicts if key doesn't exist yet
November 9, 2022 -

Made a class or plugin or whatever to append dict lists that may or may not exist.

dict[new_key].append(value)

wouldn't just create a list of there wasn't one and

dict[new_key] = []

Just clears the list.

I'm very new at this so maybe there is a way to do it in one line already, but I couldn't find it.

If you couldn't, now you can.


class Dict:

def init(self):

self.dict = {}

def append(self, key, value):

if key not in self.dict:

self.dict[key] = []

self.dict[key].append(value)


dict = Dict() dict.append(key, value)

🌐
Note.nkmk.me
note.nkmk.me › home › python
Add an Item If the Key Does Not Exist in dict in Python: setdefault | note.nkmk.me
May 6, 2023 - The setdefault() method returns the value for the specified key. If the key does not exist, a new item is added with the value from the second argument, and that value is then returned.
🌐
Finxter
blog.finxter.com › home › learn python blog › 8 best ways to update non-existing key in python dict
8 Best Ways to Update Non-Existing Key in Python Dict - Be on the Right Side of Change
May 17, 2022 - To update a key in a dictionary ... you can check if it is present in the dictionary using the in keyword along with the if statement, and then update the key-value pair using subscript notation or update() method or the asterisk * operator...
🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - The output shows that, because of the if condition, the value of c didn’t change when the dictionary was conditionally updated. You can append a dictionary or an iterable of key-value pairs to a dictionary using the update() method. The update() method overwrites the values of existing keys ...
🌐
DataCamp
datacamp.com › tutorial › python-dictionary-append
Python Dictionary Append: How to Add Key-Value Pairs | DataCamp
August 6, 2024 - In this example, the .setdefault() method adds the 'city' key with the value 'New York' because it did not already exist in the dictionary. When trying to add the 'age' key, it does not change the existing value 25 because the key already exists.
🌐
W3Schools
w3schools.com › python › python_dictionaries_add.asp
Python - Add Dictionary Items
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.
🌐
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. If the key doesn't exist, it will be added to the dictionary.
Published   July 11, 2025
Find elsewhere
🌐
CodeRivers
coderivers.org › blog › python-dictionary-add-key-value-pair-if-not-exists
Python Dictionary: Adding Key-Value Pairs If Not Exists - CodeRivers
April 12, 2025 - The simplest way to add a key-value pair to a dictionary only if the key does not exist is by using an if statement.
🌐
Replit
replit.com › home › discover › how to add a key value pair to a dictionary in python
How to add a key value pair to a dictionary in Python | Replit
For a more direct approach, the setdefault() method is perfect. It only adds the key-value pair if the key doesn't already exist, protecting your existing data from being overwritten.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Check If a Key/Value Exists in a Dictionary in Python | note.nkmk.me
May 19, 2025 - Use not in to check for absence. ... ... To check whether a specific key-value pair exists in a dictionary, use the in operator with the items() method....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-dictionary-add-value-to-existing-key
Python Dictionary Add Value to Existing Key - GeeksforGeeks
July 23, 2025 - setdefault() can be used when we want to add a key with a default value if it does not already exist and it returns the value of the key, allowing us to modify it as needed ... d = {'a': 1, 'b': 2} # Add 3 to the value of key 'a', use 0 if 'a' ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-dictionary-key
Python Add Dictionary Key - GeeksforGeeks
December 8, 2024 - 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. ... In this example, key 4 is added to the dictionary with a value of 40.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › python: how to add items to dictionary
Python: How to Add Items to Dictionary
December 22, 2025 - Provide unique keys to avoid overwriting data. 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.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can I check if a given key already exists in a Python dictionary? - Python - Data Science Dojo Discussions
April 27, 2023 - I have a Python dictionary, and I want to check if a given key already exists in it before adding a new key-value pair. What’s the best way to do this? Let’s say I want to check if the key “apple” already exists in my_dict. I know I can do this using the in operator like so: But are ...
🌐
Python.org
discuss.python.org › ideas
Enhancement About "Deep" Appending In Dicts - Ideas - Discussions on Python.org
May 24, 2022 - Proposal (Not A Huge Thing) some_dict = {} some_dict[key1][key2] = value I am speaking according to the example, I propose you to support appending in cases where some_dict[key1] doesn’t exist. To my idea, it should append an empty dict value -as default- to some_dict (with the key key1) which contains the key-value pair key2: value: some_dict == { key1:{key2:value} } Benefit Serial append which I find great for complex dict in dict structures.
🌐
Dataquest Community
community.dataquest.io › share
Just a quick one: neat way to increment dictionary key if it does not exist in one line - Share - Dataquest Community
June 6, 2020 - Hey! Hope everyone’s learning is going well. The following is what I thought was a neat snippet a friend showed me in JavaScript that can be translated to Python for incrementing a dictionary’s value at a key regardless of whether it exists already. Just started on the site so I’m not ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-new-keys-to-a-dictionary
Add new keys to a dictionary in Python - GeeksforGeeks
July 11, 2025 - Explanation: d["c"]: creates a new key "c" and its value is assigned as "3". If key "c" already exists then it will replace the existing value with new value. The update() method can be use to merge dictionaries or add multiple keys and their values in one operation. Python · 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": 5}) print(d) Output ·