← Back to Chapters

Python Change Dictionary Items

? Python Change Dictionary Items

⚡ Quick Overview

Python dictionaries are mutable, which means you can change their contents after creation. You can:

  • Update the value of an existing key using assignment.
  • Add a new key-value pair simply by assigning to a new key.
  • Use the update() method to change multiple items or merge dictionaries at once.

Understanding how to change dictionary items is essential for working with dynamic data like user profiles, settings, and configuration objects.

? Key Concepts

  • Existing key update: Assign a new value to a key that already exists.
  • Implicit add: Assigning to a new key automatically adds that key to the dictionary.
  • update() method: Change multiple keys or add new ones using another dictionary or key-value pairs.
  • Mutability: Operations modify the original dictionary rather than creating a new one.

? Syntax and Theory

? Update an existing key

dict_name[key] = new_value changes the value mapped to key.

➕ Add a new key-value pair

If key does not exist, dict_name[key] = value creates it.

? Using update()

dict_name.update(other_dict) will:

  • Overwrite values for existing keys.
  • Add new key-value pairs that were not present before.

From Python 3.7+, dictionaries preserve insertion order, which makes updates predictable in terms of order when you print or iterate over them.

? Code Examples

✏️ Updating an Existing Value

Change the value of an existing key by assigning a new value to that key.

? View Code Example (Update one key)
person = {"name": "Alice", "age": 25, "city": "New York"}

person["age"] = 26
print(person)
# {'name': 'Alice', 'age': 26, 'city': 'New York'}

➕ Adding a New Key-Value Pair

If the key does not exist, assigning a value to it will add a new key-value pair.

? View Code Example (Add new key)
person["email"] = "alice@example.com"
print(person)
# {'name': 'Alice', 'age': 26, 'city': 'New York', 'email': 'alice@example.com'}

? Using the update() Method

The update() method can modify multiple keys at once or add new key-value pairs from another dictionary.

? View Code Example (Using update)
person.update({"age": 27, "city": "Los Angeles", "phone": "123-456-7890"})
print(person)
# {'name': 'Alice', 'age': 27, 'city': 'Los Angeles', 'email': 'alice@example.com', 'phone': '123-456-7890'}

? Live Output and Explanation

? What happens step by step?

  1. Initial dictionary: {"name": "Alice", "age": 25, "city": "New York"}
  2. After updating age to 26:
    {'name': 'Alice', 'age': 26, 'city': 'New York'}
  3. After adding email:
    {'name': 'Alice', 'age': 26, 'city': 'New York', 'email': 'alice@example.com'}
  4. After calling update():
    {'name': 'Alice', 'age': 27, 'city': 'Los Angeles', 'email': 'alice@example.com', 'phone': '123-456-7890'}

Notice how the same person dictionary keeps getting modified. No new dictionary is created — you are changing the original object in memory.

? Tips and Best Practices

  • Use direct assignment when you only need to change one key-value pair.
  • Use update() when you need to modify or add multiple items at once.
  • Remember: assigning to a non-existent key will add a new key instead of raising an error.
  • From Python 3.7+, dictionaries keep insertion order, which helps when you rely on the way data is displayed or iterated.
  • When merging configurations or settings, update() is a clean way to apply overrides.

? Try It Yourself

  • Create a dictionary that represents a book (title, author, year).
  • Change the publication year using direct assignment.
  • Add a new key "genre" to your book dictionary.
  • Use update() to:
    • Change the title and year at the same time.
    • Add a new key like "rating" or "pages".
  • Create a second dictionary with some extra details and merge it into the first one using update().