T
Tech Town
Log InSign Up Free
← Back to Blog

June 19, 2025 · admin

Python Dictionaries


πŸ“– Python Dictionaries – The Smartest Way to Store Data | TechTown.in

Dictionaries in Python are powerful, fast, and super useful. They let you store data in key-value pairs, making it easy to look up, update, and organize information logically.

If you’ve ever needed a structure like a real-world dictionary β€” where each word (key) maps to its definition (value) β€” then this is exactly what Python dictionaries offer.

Let’s dive into how dictionaries work, why they’re so useful, and how to use them like a pro.


πŸ” What Is a Python Dictionary?

A dictionary in Python is an unordered, changeable, and indexed collection of data made up of key-value pairs.

βœ… Example:

person = {
    "name": "Tanmay",
    "age": 22,
    "city": "Mumbai"
}

Here:

  • "name", "age", "city" β†’ keys
  • "Tanmay", 22, "Mumbai" β†’ corresponding values

πŸ“¦ How to Create a Dictionary

my_dict = {
    "brand": "Ford",
    "model": "Mustang",
    "year": 1964
}

You can also use the dict() constructor:

my_dict = dict(brand="Ford", model="Mustang", year=1964)

πŸ”„ Accessing Values

Use the key to access the value:

print(my_dict["model"])  # Output: Mustang

Or use .get() safely (returns None if the key doesn’t exist):

print(my_dict.get("color"))  # Output: None

βœ… Dictionary Features

FeatureDescription
IndexedAccess via keys, not numeric indexes
MutableYou can change, add, or remove items
No duplicatesEach key must be unique
Fast lookupOptimized for quick access using keys

✍️ Changing and Adding Items

my_dict["year"] = 2025
my_dict["color"] = "red"

βœ… Add or update values using key assignment.


❌ Removing Items

my_dict.pop("model")
del my_dict["year"]
my_dict.clear()  # Empties the dictionary

πŸ” Looping Through Dictionaries

for key in my_dict:
    print(key, "β†’", my_dict[key])

Or:

for key, value in my_dict.items():
    print(f"{key}: {value}")

🧠 Real-World Use Cases

  • User Profiles: Store user info (name, email, roles)
  • Product Catalogs: Key = product ID, Value = details
  • APIs & JSON Data: Most web APIs return data as dictionaries
  • Database Records: Store column names and values as key-value pairs

πŸ“ Summary – Python Dictionary at a Glance

TaskSyntax Example
Create Dictionaryd = {"a": 1, "b": 2}
Access Valued["a"] or d.get("a")
Add/Update Itemd["c"] = 3
Remove Itemd.pop("b") / del d["b"]
Loop Through Keysfor k in d:
Loop Through Itemsfor k, v in d.items():

🏁 Final Thoughts

Python dictionaries are one of the most important and versatile data structures. Whether you’re working with users, files, settings, or APIs β€” you’ll likely use dictionaries every day.

Learning how to create, update, and loop through dictionaries opens doors to building real-world Python projects β€” fast and efficiently.


πŸ“˜ Ready to level up? Learn how to modify, nest, and apply powerful dictionary methods at TechTown.in