← Back to Chapters

Python Dictionary Methods

? Python Dictionary Methods

⚡ Quick Overview

Python dictionaries come with several built-in methods that make it easy to read, update, and delete data. In this topic, we focus on: get(), keys(), values(), items(), update(), pop(), popitem(), and clear().

? Key Concepts

  • get() safely reads a value without raising an error if the key is missing.
  • keys(), values(), and items() return view objects for iterating over the dictionary.
  • update() merges or modifies key–value pairs.
  • pop() removes a specific key and returns its value.
  • popitem() removes and returns the last inserted key–value pair (in Python 3.7+).
  • clear() removes all items but keeps the dictionary object itself.

? Syntax and Theory

  • dict.get(key, default=None) → returns value if key exists, else default.
  • dict.keys() → view of all keys.
  • dict.values() → view of all values.
  • dict.items() → view of (key, value) pairs.
  • dict.update(other_dict_or_iterable) → updates/extends dictionary.
  • dict.pop(key[, default]) → removes key and returns its value.
  • dict.popitem() → removes and returns last inserted (key, value) pair.
  • dict.clear() → removes all items.

? Code Examples

? Using get()

? View Code Example
person = {"name": "Alice", "age": 25}

print(person.get("name"))          # Alice
print(person.get("city", "N/A"))   # N/A

?️ Working with keys()

? View Code Example
person = {"name": "Alice", "age": 25}

print(person.keys())  # dict_keys(['name', 'age'])

for key in person.keys():
    print(key)

? Working with values()

? View Code Example
person = {"name": "Alice", "age": 25}

print(person.values())  # dict_values(['Alice', 25])

for value in person.values():
    print(value)

? Working with items()

? View Code Example
person = {"name": "Alice", "age": 25}

print(person.items())  # dict_items([('name', 'Alice'), ('age', 25)])

for key, value in person.items():
    print(key, "→", value)

➕ Updating with update()

? View Code Example
person = {"name": "Alice", "age": 25}

person.update({"city": "New York", "age": 26})
print(person)
# {'name': 'Alice', 'age': 26, 'city': 'New York'}

? Removing with pop()

? View Code Example
person = {"name": "Alice", "age": 26, "city": "New York"}

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

? Removing last item with popitem()

? View Code Example
person = {"name": "Alice", "age": 26, "city": "New York"}

item = person.popitem()
print(item)    # ('city', 'New York')
print(person)  # {'name': 'Alice', 'age': 26}

? Clearing with clear()

? View Code Example
person = {"name": "Alice", "age": 26}

person.clear()
print(person)  # {}

? Live Output / Explanation

? What the code prints

From the examples above, we get outputs like:

  • get("name")Alice
  • get("city", "N/A")N/A (because city does not exist yet)
  • keys()dict_keys(['name', 'age'])
  • values()dict_values(['Alice', 25])
  • items()dict_items([('name', 'Alice'), ('age', 25)])
  • After update(){'name': 'Alice', 'age': 26, 'city': 'New York'}
  • pop("age") → returns 26 and removes the key age.
  • popitem() → returns the last inserted pair, e.g. ('city', 'New York').
  • clear() → dictionary becomes {}.

Notice how get() never crashes if the key is missing (it simply returns the default), while pop() will raise an error if the key is not found and no default is provided.

? Tips & Best Practices

  • Use get() instead of dict[key] when a key might not exist.
  • Use update() to merge dictionaries or override existing keys cleanly.
  • keys(), values(), and items() are very useful when looping over data.
  • Use pop() when you know the key you want to remove.
  • Use popitem() when you want to treat the dictionary like a stack of the latest entries.
  • clear() empties the dictionary but keeps the same object in memory.

? Try It Yourself

  • Create a dictionary representing a student (name, age, course, city) and access a key using get().
  • Loop through keys(), values(), and items() and print them nicely.
  • Use update() to add a new key like "grade" and modify an existing key.
  • Remove a key using pop() and observe the returned value.
  • Call popitem() on your dictionary and see which key–value pair is removed.
  • Finally, call clear() and verify that the dictionary is empty.