Python provides several built-in methods for working with dictionaries. These methods help you access, add, update, remove, and copy dictionary data.
| Method | Purpose |
|---|---|
| get() | Returns the value of a specified key |
| keys() | Returns all dictionary keys |
| values() | Returns all dictionary values |
| items() | Returns key-value pairs |
| update() | Adds or updates dictionary items |
| pop() | Removes a specified key and returns its value |
| popitem() | Removes and returns the last inserted key-value pair |
| clear() | Removes all dictionary items |
| copy() | Creates a copy of the dictionary |
| setdefault() | Returns a value and optionally inserts a default value |
The get() method returns the value associated with a specified key.
student = {
"name": "Amit",
"age": 20
}
print(student.get("name"))
If the key does not exist, get() returns None by default.
student = {
"name": "Amit"
}
print(student.get("age"))
You can provide a default value that will be returned when the key does not exist.
student = {
"name": "Amit"
}
print(student.get("age", 18))
The keys() method returns a view containing all dictionary keys.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(student.keys())
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
for key in student.keys():
print(key)
The values() method returns a view containing all dictionary values.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(student.values())
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
for value in student.values():
print(value)
The items() method returns a view containing all key-value pairs.
student = {
"name": "Amit",
"age": 20
}
print(student.items())
student = {
"name": "Amit",
"age": 20
}
for key, value in student.items():
print(key, ":", value)
The update() method adds new key-value pairs or updates existing keys.
student = {
"name": "Amit",
"age": 20
}
student.update({"course": "Python"})
print(student)
If the key already exists, update() changes its value.
student = {
"name": "Amit",
"age": 20
}
student.update({"age": 21})
print(student)
The pop() method removes the specified key and returns its associated value.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
age = student.pop("age")
print(age)
print(student)
You can provide a default value to pop(). This avoids a KeyError when the specified key is missing.
student = {
"name": "Amit"
}
result = student.pop("age", "Not Found")
print(result)
The popitem() method removes and returns the last inserted key-value pair.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
item = student.popitem()
print(item)
print(student)
Calling popitem() on an empty dictionary raises a KeyError.
student = {}
student.popitem()
The clear() method removes all key-value pairs from a dictionary.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
student.clear()
print(student)
The copy() method creates a shallow copy of a dictionary.
student = {
"name": "Amit",
"age": 20
}
new_student = student.copy()
print(new_student)
Changing a separate dictionary created with copy() does not change the top-level contents of the original dictionary.
student = {
"name": "Amit",
"age": 20
}
new_student = student.copy()
new_student["age"] = 21
print("Original:", student)
print("Copy:", new_student)
The setdefault() method returns the value of a key. If the key does not exist, it inserts the key with a specified default value.
student = {
"name": "Amit"
}
age = student.setdefault("age", 20)
print(age)
print(student)
If the key already exists, setdefault() returns its existing value and does not replace it with the default value.
student = {
"name": "Amit",
"age": 20
}
result = student.setdefault("age", 25)
print(result)
print(student)
The fromkeys() method creates a new dictionary using the specified keys and an optional common value.
keys = ("name", "age", "course")
student = dict.fromkeys(keys)
print(student)
keys = ("name", "age", "course")
student = dict.fromkeys(keys, "Not Available")
print(student)
| Feature | del | pop() |
|---|---|---|
| Removes item | Yes | Yes |
| Returns removed value | No | Yes |
| Can provide default value | No | Yes |
student = {
"name": "Rahul",
"course": "Python",
"fees": 5000
}
print("Name:", student.get("name"))
student.update({"fees": 6000})
print("Updated Fees:", student["fees"])
for key, value in student.items():
print(key, ":", value)
Question: Which dictionary method returns all key-value pairs?