Dictionary Methods
Dictionaries in Python provide various built-in methods to manipulate and access data
efficiently.
📌 len() → Get the number of key-value pairs
📌 dict() → Create a dictionary
📌 keys() → Get all keys
📌 values() → Get all values
📌 items() → Get all key-value pairs
📌 get() → Retrieve a value safely
📌 update() → Merge another dictionary
📌 del → Remove a specific key
📌 clear() → Remove all elements
📌 fromkeys() → Create a dictionary from keys
📌 copy() → Create a duplicate dictionary
📌 pop() → Remove a key and return its value
📌 popitem() → Remove the last inserted key-value pair
📌 setdefault() → Get a value or set a default
📌 max(), min() → Find the largest/smallest key
📌 sorted() → Sort dictionary keys
1. len() → Get the Number of Key-Value Pairs
Returns the total number of items in a dictionary.
student = {"name": "Ajay", "age": 25, "course": "Python"}
print(len(student))
✅ Output:
3
2. dict() → Create a Dictionary
Creates a dictionary using the dict() function.
student = dict(name="Ajay", age=25, course="Python")
print(student)
✅ Output:
{'name': 'Ajay', 'age': 25, 'course': 'Python'}
3. keys() → Get All Keys
Returns a list-like object containing all keys.
student = {"name": "Ajay", "age": 25, "course": "Python"}
print([Link]())
✅ Output:
dict_keys(['name', 'age', 'course'])
4. values() → Get All Values
Returns a list-like object containing all values.
print([Link]())
✅ Output:
dict_values(['Ajay', 25, 'Python'])
5. items() → Get All Key-Value Pairs
Returns a list-like object containing tuples of key-value pairs.
print([Link]())
✅ Output:
dict_items([('name', 'Ajay'), ('age', 25), ('course', 'Python')])
6. get() → Retrieve a Value Safely
Returns the value of a key without errors if the key is missing.
print([Link]("name"))
print([Link]("grade", "Not Available"))
✅ Output:
Ajay
Not Available
7. update() → Merge Another Dictionary
Updates the dictionary with another dictionary.
[Link]({"grade": "A", "age": 26})
print(student)
✅ Output:
{'name': 'Ajay', 'age': 26, 'course': 'Python', 'grade': 'A'}
8. del → Remove a Specific Key
Deletes a key-value pair.
del student["grade"]
print(student)
✅ Output:
{'name': 'Ajay', 'age': 26, 'course': 'Python'}
9. clear() → Remove All Elements
Removes all key-value pairs.
[Link]()
print(student)
✅ Output:
{}
10. fromkeys() → Create a Dictionary from Keys
Creates a dictionary with default values.
keys = ["name", "age", "course"]
default_dict = [Link](keys, "Unknown")
print(default_dict)
✅ Output:
{'name': 'Unknown', 'age': 'Unknown', 'course': 'Unknown'}
11. copy() → Create a Duplicate Dictionary
Creates a shallow copy of a dictionary.
student_copy = [Link]()
print(student_copy)
✅ Output:
{'name': 'Ajay', 'age': 26, 'course': 'Python'}
12. pop() → Remove a Key and Return Its Value
Removes a key and returns its value.
removed_value = [Link]("age")
print(f"Removed Value: {removed_value}")
print(student)
✅ Output:
Removed Value: 26
{'name': 'Ajay', 'course': 'Python'}
13. popitem() → Remove the Last Inserted Key-Value Pair
Removes the last inserted key-value pair.
[Link]()
print(student)
✅ Output:
{'name': 'Ajay'}
14. setdefault() → Get a Value or Set a Default
Returns the value of a key, or sets a default if the key is missing.
grade = [Link]("grade", "B")
print(grade)
print(student)
✅ Output:
B
{'name': 'Ajay', 'age': 26, 'course': 'Python', 'grade': 'B'}
15. max() & min() → Find Largest & Smallest Key
Finds the largest and smallest key in a dictionary.
scores = {"Alice": 85, "Bob": 92, "Charlie": 78}
print(max(scores))
print(min(scores))
✅ Output:
Charlie
Alice
16. sorted() → Sort Dictionary Keys
Returns a sorted list of keys.
print(sorted(scores))
✅ Output:
['Alice', 'Bob', 'Charlie']