Computer Science

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() with values().

CBSE Important Programs

  1. Display all keys of a dictionary.
  2. Display all values of a dictionary.
  3. Display all key-value pairs.
  4. Safely retrieve a value using get().
  5. Update marks using update().
  6. Delete a key using pop().
  7. Delete the last inserted item using popitem().
  8. Clear a dictionary.
  9. Count the number of key-value pairs.
  10. Traverse a dictionary using items().

Viva Questions

  1. What is the purpose of the keys() method?
  2. What does the values() method return?
  3. How is items() different from keys()?
  4. What is the advantage of get() over the [] operator?
  5. What is the purpose of the update() method?
  6. What does clear() do?
  7. How is pop() different from popitem()?
  8. What does len() return for a dictionary?
  9. Can update() add new keys?
  10. Which method is safest for accessing an optional key?

Exam Tips

  • Use get() when a key may not exist.
  • Remember that keys(), values(), and items() return dictionary view objects.
  • update() can both modify existing keys and add new ones.
  • pop() removes a specified key, whereas popitem() 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.