Friday, January 16, 2026

Python Dictionary

 

Removing Dictionary Items

Dictionary items can be removed using built-in deletion methods that work on keys:

  • del: removes an item using its key
  • pop(): removes the item with the given key and returns its value
  • clear(): removes all items from the dictionary
  • popitem(): removes and returns the last inserted key–value pair

Output
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Geeks
Key: 3, Value: Geeks
{}

Iterating Through a Dictionary

A dictionary can be traversed using a for loop to access its keys, values or both key-value pairs by using the built-in methods keys()values() and items().


Output
1
2
age
Geeks
For
22
1: Geeks
2: For
age: 22

No comments:

Post a Comment

Python Dictionary

  Removing Dictionary Items Dictionary items can be removed using built-in deletion methods that work on keys: del :  removes an item using ...