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().
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.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.
person = {"name": "Alice", "age": 25}
print(person.get("name")) # Alice
print(person.get("city", "N/A")) # N/A
person = {"name": "Alice", "age": 25}
print(person.keys()) # dict_keys(['name', 'age'])
for key in person.keys():
print(key)
person = {"name": "Alice", "age": 25}
print(person.values()) # dict_values(['Alice', 25])
for value in person.values():
print(value)
person = {"name": "Alice", "age": 25}
print(person.items()) # dict_items([('name', 'Alice'), ('age', 25)])
for key, value in person.items():
print(key, "→", value)
person = {"name": "Alice", "age": 25}
person.update({"city": "New York", "age": 26})
print(person)
# {'name': 'Alice', 'age': 26, 'city': 'New York'}
person = {"name": "Alice", "age": 26, "city": "New York"}
age = person.pop("age")
print(age) # 26
print(person) # {'name': 'Alice', 'city': 'New York'}
person = {"name": "Alice", "age": 26, "city": "New York"}
item = person.popitem()
print(item) # ('city', 'New York')
print(person) # {'name': 'Alice', 'age': 26}
person = {"name": "Alice", "age": 26}
person.clear()
print(person) # {}
From the examples above, we get outputs like:
get("name") → Aliceget("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)])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.
get() instead of dict[key] when a key might not exist.update() to merge dictionaries or override existing keys cleanly.keys(), values(), and items() are very useful when looping over data.pop() when you know the key you want to remove.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.get().keys(), values(), and items() and print them nicely.update() to add a new key like "grade" and modify an existing key.pop() and observe the returned value.popitem() on your dictionary and see which key–value pair is removed.clear() and verify that the dictionary is empty.