🗂️ Dictionary Data Type

Last Updated: 17th August 2025


  • A dictionary is an unordered collection of key : value pairs.
  • Keys must be unique and immutable (string, number, tuple).
  • Values can be any type (number, string, list, tuple, etc.).
  • Dictionaries are mutable — you can change, add, or remove items.

Hinglish Tip 🗣: Dictionary ko socho ek phonebook jaisa — name (key) aur number (value). Key se value directly milti hai.


✏ Creating Dictionaries

# empty dictionary
d = {}

# literal creation
person = {"name": "Asha", "age": 25, "city": "Varanasi"}

# using dict() constructor
p2 = dict(name="Rahul", age=30)

# keys can be tuples (immutable)
coords = { (0,0): "origin", (1,2): "point" }

🔎 Accessing Items

d = {"name": "Sadhu", "age": 2, "city": "Varanasi"}
d["name"]  # "Sadhu"
d["age"]   # 2
d["city"]  # "Varanasi"

print(d)  # {'name': 'Sadhu', 'age': 2, 'city': 'Varanasi'}

📝 Modifying Items

d = {"name": "Sadhu", "age": 2, "city": "Varanasi"}
d["city"] = "Kashi"
print(d)  # {'name': 'Sadhu', 'age': 2, 'city': 'Kashi'}

💡 Quick Practice

  • Create a dictionary with your name, age, and city.
  • Update your city to "Ayodhya".
  • Print the dictionary.
  • Exercise