Originally published on CodingCush.
Python dictionary methods are the built-in tools for working with key and value data: get() reads a value safely, keys(), values() and items() let you loop through a dictionary, update() adds or changes several pairs at once, pop() removes a key and gives you its value, and setdefault() adds a key only if it is missing. Learn these few methods well, and dictionaries stop feeling scary and start feeling like the helpful notebook they really are.
A Quick Refresher: What a Dictionary Holds
A dictionary stores pairs: a key on the left, a value on the right, inside curly braces. You look things up by key, not by position. That is why a dictionary is perfect for real-world records — a student has a name, an age and a city, and you want each piece labelled, not numbered.
student = { "name": "Asha", "age": 21, "city": "Delhi" } print(student["name"]) # Asha print(student["age"]) # 21 This labelling idea is exactly why dictionaries show up everywhere in Python, from web API responses to configuration files. If you are still mixing up when to reach for a list and when to reach for a dictionary, our comparison of Python list vs dictionary gives a simple rule you can follow. The official Python tutorial introduces dictionaries in its Data Structures chapter, and the Python Tutorial Data Structures page explains that dictionaries are indexed by keys, which can be any immutable type, such as strings and numbers.
get(): Read a Value Without Crashing
The most useful method for beginners is get(). Reading a missing key with square brackets raises a KeyError and stops your program — a bit like asking a shopkeeper for something they do not sell and having the shop close in protest. get() is politer: it returns None, or a default value you choose, if the key is missing.
print(student.get("name")) # Asha print(student.get("marks")) # None print(student.get("marks", 0)) # 0 Use the bracket style when the key must exist and a missing key means a real bug. Use get() when the key is optional — user input, API data, or settings that may not be set yet. This one habit prevents a whole category of beginner crashes, and the screenshot below is from the very tutorial page that recommends it.
The official Python Tutorial Data Structures page, section 5.5 "Dictionaries", on docs.python.org.
keys(), values() and items(): Look Inside
These three methods give you views of the dictionary, and they are the standard way to loop through one. keys() gives the keys, values() gives the values, and items() gives both together as pairs — the one you will use most often in real code.
for key in student.keys(): print(key) for value in student.values(): print(value) for key, value in student.items(): print(key, "=>", value) A small table helps you remember which is which:
| Method | Returns | Typical use |
|---|---|---|
| keys() | All keys | Checking what a record contains |
| values() | All values | Adding marks, prices, totals |
| items() | Key and value pairs | Printing a record, looping with both |
| get(key, default) | One value, safely | Optional data that may be missing |
Notice that these views update themselves if the dictionary changes later. That surprises some beginners, but it is handy: a view is a live window into the dictionary, not a frozen photograph of it.
update(): Add or Change Several Pairs at Once
To change one pair, simple assignment is fine: student["age"] = 22. To change several at once, update() is cleaner. It takes another dictionary (or pairs) and merges it in — existing keys get their new values, new keys get added.
student.update({"age": 22, "course": "Python"}) print(student) # {'name': 'Asha', 'age': 22, 'city': 'Delhi', 'course': 'Python'} This is the pattern behind settings screens and profile editors: collect the changes, apply them in one go, and carry on. The full list of dictionary operations, including a few advanced ones, lives in the Python standard types documentation, which is worth bookmarking once these basics feel comfortable.
pop(), popitem() and del: Removing Things
There are three honest ways to remove data. pop(key) removes a key and returns its value, which is useful when you want to use the value one last time. popitem() removes the most recently added pair — handy for simple stacks and queues built on dictionaries. And the del statement removes a key without returning anything.
city = student.pop("city") print(city) # Delhi print(student) # city is gone del student["course"] print(student) Be careful: pop() on a missing key raises an error, just like bracket reading. Give it a default — student.pop("city", "Unknown") — and it will stay calm when the key is not there. Small defaults like this are the difference between a program that crashes in front of a user and one that quietly does something sensible.
setdefault(): Add Only If Missing
The last method beginners should know is setdefault(). It reads a key if it exists, and if it does not, it creates it with a default value and returns that. It shines when you are grouping data — counting words, or collecting students by course.
groups = {} groups.setdefault("Python", []).append("Asha") groups.setdefault("Python", []).append("Ravi") groups.setdefault("PHP", []).append("Neha") print(groups) # {'Python': ['Asha', 'Ravi'], 'PHP': ['Neha']} Without setdefault(), that code needs an if-check before every append. With it, the grouping logic fits in three readable lines. This grouping pattern connects nicely to the idea behind a hash table or HashMap, because a Python dictionary is a hash table under the hood — which is also why lookups by key stay fast even when the dictionary grows large.
Nested Dictionaries: Records Inside Records
Real data is rarely flat. A student record might contain another dictionary for the address, or a list of marks. Python handles this naturally, because a value can be any type — including another dictionary. You read nested values by chaining keys, and you can still use get() at each level to stay safe when a level might be missing.
student = { "name": "Asha", "address": {"city": "Delhi", "pin": "110001"} } print(student["address"]["city"]) # Delhi print(student.get("address", {}).get("state", "Unknown")) That second line looks clever, but use it sparingly — clarity beats cleverness when you return to the code next month. Nested dictionaries are exactly the shape of JSON data from web APIs, which is why learning to walk through them comfortably in small examples pays off so quickly in real projects, from weather apps to shopping sites.
A Small Practice Task
Try this before you move on: build a dictionary for a shop item with name, price and stock. Use get() to print a discount key that does not exist yet, with a default of zero. Use update() to change the price and add the discount. Loop with items() and print the final record neatly. If you can do those four moves without looking back at this page, you genuinely know dictionary methods — not just in theory, but in your fingers, which is where programming knowledge actually lives.
What is the difference between get() and square brackets?
Square brackets raise a KeyError if the key is missing. get() returns None, or a default you choose, instead. Use brackets when the key must exist, and get() when it might not.
How do I check if a key exists in a dictionary?
Use the in keyword: "name" in student returns True or False. It checks keys only, not values, and it is fast and readable.
Can a dictionary have two values for the same key?
No. Keys are unique. If you assign to an existing key, the old value is replaced. If you need several values under one key, store a list as the value, like the grouping example above.
Are dictionaries ordered in Python?
Yes. Since Python 3.7, dictionaries remember the order in which keys were inserted, and loops follow that order. You can rely on it in modern Python.
Conclusion
Dictionary methods are a small set with a big payoff: get() for safe reading, keys(), values() and items() for looking inside, update() for merging changes, pop() for tidy removal, and setdefault() for grouping. Practise them on one small record of your own, and dictionaries will quickly become the Python tool you reach for first.
