Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
To add an entry to a Python dictionary, assign a value to a key: d[key] = value. Dictionaries do not have an append() method. If the key already exists, assignment replaces its value; to collect several values under one key, make that value a list and append to the list instead.
Add or replace one dictionary entry
A dictionary maps unique keys to values. Assignment creates a key when it is new and changes the associated value when the key already exists.
scores = {}
scores["Ada"] = 95 # add a key/value pair
scores["Ada"] = 98 # replace the value for "Ada"
After these statements, scores contains one entry: {"Ada": 98}. It does not contain two entries for the same key. See the Python dictionary documentation.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Add or update several entries
Use update() to apply several pairs from a mapping or an iterable of key/value pairs. It mutates the dictionary in place, replaces values for keys that already exist, and returns None.
#1 Best Overall
settings = {"theme": "light"}
settings.update({"theme": "dark", "language": "en"})
print(settings)
# {'theme': 'dark', 'language': 'en'}
The theme value is replaced, while language is added. The documentation for dict.update() lists the accepted input forms.
Append values under one key
If you mean to keep multiple values associated with the same key, store a list as the dictionary value. Call append() on that list—not on the dictionary.
Rank #2
Use setdefault()
setdefault(key, default) returns the value already associated with a key, or inserts the default and returns it if the key is absent. This makes it convenient to initialize a list before appending:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsgroups = {}
groups.setdefault("fruit", []).append("pear")
groups.setdefault("fruit", []).append("plum")
print(groups)
# {'fruit': ['pear', 'plum']}
Use defaultdict(list)
When many keys may need lists, collections.defaultdict can create an empty list automatically the first time each missing key is accessed:
from collections import defaultdict
groups = defaultdict(list)
groups["fruit"].append("pear")
groups["fruit"].append("plum")
Choose defaultdict(list) when automatic list creation is useful. Choose setdefault() when you want to keep a regular dictionary explicit. Both patterns are documented in the standard-library examples for defaultdict.
Read a key without inserting it
Use get() when a key may be missing and you want a fallback without changing the dictionary. A bracket lookup raises KeyError if the key is absent.
settings = {"theme": "dark"}
font = settings.get("font", "system") # returns "system"; no entry is added
# settings["font"] would raise KeyError
get() is for reading with a fallback; it does not insert that fallback. Use setdefault() when you want a missing key to be created.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteChoose the operation that matches the goal
- Add or replace one pair:
d[key] = value. - Apply multiple pairs:
d.update(other). - Insert a default only if absent:
d.setdefault(key, default). - Collect repeated values for one key: store a list and call
.append()on that list. - Read with a fallback without inserting:
d.get(key, default).
What happens to dictionary order?
Current Python dictionaries preserve insertion order. Updating an existing key does not move it; deleting a key and then inserting it again places it at the end. Order does not change the rule that a key has one associated value at a time.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

