October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

Add Items to a Python Dictionary: Use Assignment, Not append()

Use d[key] = value to add a pair to a Python dictionary. For multiple items under one key, store a list and append to that list.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Python dictionaries have no append() method. To add a key-value pair, assign a value with d[key] = value. If the key already exists, the assignment replaces its value; it does not create a duplicate key.

append() is for a list value, not a dictionary

A dictionary maps unique keys to values. That means adding an item to the dictionary means creating a key-value pair, which you do with assignment. The name “append” is often used informally to mean “add,” but calling append() on a dictionary raises an error because dictionaries do not provide that method. See the Python dictionary documentation.

As an Amazon Associate I earn from qualifying purchases.

scores = {}
scores["Ada"] = 95

scores["Ada"] = 98  # Replaces 95; it does not add another "Ada" entry

Add one key-value pair

Use square-bracket assignment when you want a key to point to a value:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
settings = {"theme": "light"}
settings["language"] = "en"  # Adds a new key
settings["theme"] = "dark"   # Replaces the existing value

Dictionary keys are unique. Assigning to a key that is already present updates the value associated with that key.

Add or update several pairs

Use update() to apply pairs from another mapping or an iterable of key-value pairs. It changes the dictionary in place and returns None; matching keys are overwritten.

settings.update({"theme": "dark", "language": "en"})

Collect multiple values under one key

If your goal is to keep several values associated with one key, make the dictionary value a list. Then call append() on that list—not on the dictionary.

groups = {}
groups.setdefault("fruit", []).append("pear")
groups.setdefault("fruit", []).append("plum")

# {"fruit": ["pear", "plum"]}

setdefault(key, default) returns the existing value for a key, or inserts and returns default when the key is absent. Here, it provides a list that can receive the new item.

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

When you repeatedly build lists for keys that may not exist yet, collections.defaultdict(list) is another option:

from collections import defaultdict

groups = defaultdict(list)
groups["fruit"].append("pear")
groups["fruit"].append("plum")

Use setdefault() when you want to keep a regular dictionary explicit; use defaultdict(list) when automatic list creation for missing keys suits the code. Both patterns are documented in the Python collections documentation.

Read a missing key without inserting it

Square-bracket lookup raises KeyError when the key is absent. If absence is expected and you want a fallback value without changing the dictionary, use get():

value = settings.get("font", "system")

If "font" is missing, value is "system"; get() does not add a "font" entry. By contrast, setdefault("font", "system") would insert that key when it is absent.

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

Choose the operation that matches the task

What you want Use Effect
Add or replace one pair d[key] = value Creates the key if absent; replaces its value if present.
Apply several pairs d.update(other) Mutates d; overwrites colliding keys.
Insert a fallback only if absent d.setdefault(key, default) Returns the existing value or inserts and returns the default.
Collect repeated items under one key d[key].append(item) Appends to a list stored as that key’s value; initialize the list with setdefault() or defaultdict(list) if needed.
Read with a fallback, without inserting d.get(key, default) Returns the stored value or the fallback; leaves the dictionary unchanged.
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. See the dictionary type documentation for these ordering details.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.