Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
groups = {}
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose 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).
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.