Academy

Dictionaries

When a List Isn't Enough

A list stores values in order, accessed by position:

person = ["Priya", 28, "Mumbai"]
print(person[0])   # Name? Address? Who knows.

This works, but it's fragile. You must remember that index 0 is the name, 1 is the age, 2 is the city. The moment the list changes order, everything breaks. And a reader seeing person[2] has no idea what they're looking at.

What you actually want is to label each piece of information. Not position 0 but name. Not position 2 but city.

That's exactly what a dictionary does.

Creating a Dictionary

person = {"name": "Priya", "age": 28, "city": "Mumbai"}

Curly braces. Each item is a key-value pair separated by a colon. Pairs are separated by commas.

  • The key is the label — always a string (for now).
  • The value is the data attached to that label — any type.

Accessing Values

You access values using their key, in square brackets — similar to list indexing, but with a label instead of a number.

person["name"] means: go to this dictionary, find the key "name", and return its value. Clear, readable, unambiguous.

If you ask for a key that doesn't exist, Python raises a KeyError. The safer way to access a value when you're unsure if the key exists is .get():

.get() never crashes — it returns None (or your chosen default) when the key is absent.

Adding and Changing Values

Dictionaries are mutable. You can add new pairs or change existing values at any time.

The syntax is the same for both operations. If the key already exists, the value is updated. If it doesn't, a new pair is created.

Removing Items

del removes the key-value pair entirely. The key and its value are both gone.

Checking if a Key Exists

The in keyword works on dictionaries too — it checks for keys.

Always use in before accessing a key if you're not certain it exists — it's cleaner than using .get() in every case.

Iterating Over a Dictionary

Looping through a dictionary gives you each key in turn:

If you want keys and values together without the extra lookup, use .items():

.items() gives you each pair as two variables at once. This is the standard, clean way to iterate over a dictionary when you need both the key and the value.

You can also get just the keys or just the values:

Dictionaries of Dictionaries

Values can be any type — including other dictionaries. This is how you represent richer, more structured data.

students["Priya"] returns the inner dictionary. ["grade"] then accesses within that. You'll see this pattern constantly when working with real-world data.

A Practical Example

A dictionary as a shopping cart — items as keys, prices as values. Two functions, each with one job. The += in the loop is shorthand for total = total + price. Everything from the past ten lessons working together.

What You Have Learned

Dictionaries let you store and retrieve data by meaningful labels rather than arbitrary positions.

The key ideas:

  • A dictionary stores key-value pairs: {"key": value}
  • Access values with dict["key"]; use .get() for safe access without crashing
  • Dictionaries are mutable — add, update, and delete pairs freely
  • in checks whether a key exists
  • Loop with for key in dict: or for key, value in dict.items():
  • Values can be any type, including other dictionaries

You've Completed the Beginner Series

Ten lessons. You now know how to store data in variables, work with four basic types, perform calculations, read user input, make decisions, repeat operations with loops, organise code into functions, manipulate text, and structure information in lists and dictionaries.

These are not beginner concepts in the sense of being trivial — they are the core of the language. Everything that follows in the Intermediate and Advanced series builds directly on what you've just learned.

The next series starts where this one ends: with functions that remember state, collections that organise complex data, and code that is structured to grow.