Python Dictionary Functions & Methods (Part 2) | len(), keys(), values(), items(), get(), update(), clear(), pop(), popitem() | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python Dictionary Functions & Methods (Part 2)
Python provides several built-in functions and dictionary methods to retrieve, modify, update, and manage dictionary data efficiently. These methods simplify data processing and are frequently used in real-world Python applications as well as CBSE practical examinations.
Learning Objectives
- Find the size of a dictionary.
- Retrieve keys, values, and key-value pairs.
- Safely access dictionary values.
- Update and merge dictionaries.
- Delete dictionary elements.
- Clear all dictionary contents.
1. len() Function
The len() function returns the total number of key-value pairs present in a dictionary.
Syntax
len(dictionary)
Example
student={
"roll":101,
"name":"Rahul",
"marks":95
}
print(len(student))
Output
3
2. keys() Method
The keys() method returns a view containing all the keys of a dictionary.
Syntax
dictionary.keys()
Example
student={
"roll":101,
"name":"Rahul",
"marks":95
}
print(student.keys())
Output
dict_keys(['roll','name','marks'])
3. values() Method
The values() method returns all values stored in a dictionary.
Syntax
dictionary.values()
Example
student={
"roll":101,
"name":"Rahul",
"marks":95
}
print(student.values())
Output
dict_values([101,'Rahul',95])
4. items() Method
The items() method returns all key-value pairs as tuples.
Syntax
dictionary.items()
Example
student={
"roll":101,
"name":"Rahul",
"marks":95
}
print(student.items())
Output
dict_items([('roll',101),('name','Rahul'),('marks',95)])
Traversing Using items()
student={
"roll":101,
"name":"Rahul",
"marks":95
}
for key,value in student.items():
print(key,":",value)
Output
roll : 101
name : Rahul
marks : 95
5. get() Method
The get() method returns the value associated with the specified key. If the key does not exist, it returns None (or a default value if provided) instead of generating an error.
Syntax
dictionary.get(key)
Example 1
student={
"name":"Rahul",
"marks":95
}
print(student.get("name"))
Output
Rahul
Example 2
student={
"name":"Rahul"
}
print(student.get("city"))
Output
None
Example 3
student={
"name":"Rahul"
}
print(student.get("city","Not Available"))
Output
Not Available
Difference between [] and get()
| [] Operator | get() |
|---|---|
| Raises KeyError if key is absent. | Returns None or a default value. |
| Used when key is expected to exist. | Safer for unknown keys. |
6. update() Method
The update() method updates existing key-value pairs and also adds new key-value pairs if the keys are not present.
Syntax
dictionary.update(other_dictionary)
Example
student={
"name":"Rahul",
"marks":95
}
student.update({"city":"Jaipur","marks":98})
print(student)
Output
{'name':'Rahul','marks':98,'city':'Jaipur'}
7. clear() Method
The clear() method removes all key-value pairs from the dictionary.
Syntax
dictionary.clear()
Example
student={
"roll":101,
"name":"Rahul"
}
student.clear()
print(student)
Output
{}
8. pop() Method
The pop() method removes the specified key and returns its corresponding value.
Syntax
dictionary.pop(key)
Example
student={
"name":"Rahul",
"marks":95
}
value=student.pop("marks")
print(value)
print(student)
Output
95
{'name':'Rahul'}
9. popitem() Method
The popitem() method removes and returns the last inserted key-value pair from the dictionary.
Syntax
dictionary.popitem()
Example
student={
"roll":101,
"name":"Rahul",
"marks":95
}
print(student.popitem())
print(student)
Output
('marks',95)
{'roll':101,'name':'Rahul'}
Summary Table
| Function / Method | Purpose |
|---|---|
len() |
Returns the number of key-value pairs. |
keys() |
Returns all keys. |
values() |
Returns all values. |
items() |
Returns all key-value pairs. |
get() |
Returns the value for a key safely. |
update() |
Updates or adds key-value pairs. |
clear() |
Removes all dictionary elements. |
pop() |
Removes a specified key. |
popitem() |
Removes the last inserted key-value pair. |
Real-Life Applications
- Updating student records.
- Displaying all employee details.
- Managing product information.
- Deleting outdated records.
- Merging configuration settings.
- Safely retrieving optional information.
Common Programming Mistakes
- Using
[]for a key that does not exist. - Assuming
keys()returns a list instead of a dictionary view. - Using
pop()with a missing key. - Expecting
clear()to delete the dictionary variable itself. - Confusing
items()withvalues().
CBSE Important Programs
- Display all keys of a dictionary.
- Display all values of a dictionary.
- Display all key-value pairs.
- Safely retrieve a value using
get(). - Update marks using
update(). - Delete a key using
pop(). - Delete the last inserted item using
popitem(). - Clear a dictionary.
- Count the number of key-value pairs.
- Traverse a dictionary using
items().
Viva Questions
- What is the purpose of the
keys()method? - What does the
values()method return? - How is
items()different fromkeys()? - What is the advantage of
get()over the[]operator? - What is the purpose of the
update()method? - What does
clear()do? - How is
pop()different frompopitem()? - What does
len()return for a dictionary? - Can
update()add new keys? - Which method is safest for accessing an optional key?
Exam Tips
- Use
get()when a key may not exist. - Remember that
keys(),values(), anditems()return dictionary view objects. update()can both modify existing keys and add new ones.pop()removes a specified key, whereaspopitem()removes the last inserted pair.clear()empties the dictionary but does not delete the variable.
Quick Revision
len()counts key-value pairs.keys()returns all keys.values()returns all values.items()returns key-value pairs.get()safely accesses values.update()updates or adds data.clear()empties the dictionary.pop()removes a specified key.popitem()removes the last inserted key-value pair.