โ† Back to Python Course
๐Ÿ“ฆ Lists & Dataยท25 minยทIntermediate
๐Ÿ“–

Dictionaries

Key-and-value storage

Learn dictionaries โ€” a powerful way to store data using named keys instead of numbered indexes.

๐Ÿ“–

What we are learning

A dictionary stores data in key-value pairs. Instead of accessing items by position (like lists), you access them by name. It is like having a real dictionary โ€” you look up a word (key) and find its meaning (value).

๐Ÿ’ก Think of it like this:

Think of a dictionary like a contact list in your phone. You look up someone's name (the key) and find their phone number (the value). You do not need to know what position they are in โ€” you just search by name.

๐ŸŒŸ

Why it matters

Dictionaries are perfect for storing related information. A game character might have keys like "name", "health", "level", and "score". Dictionaries keep this data organized and easy to access.

Where you see this in real life:

  • โœ“Phone contacts store names and phone numbers
  • โœ“Games store character stats (health, mana, strength)
  • โœ“Websites store user profiles with usernames and details
๐ŸŽฏ

By the end, you can:

  • 1.Create a dictionary with key-value pairs
  • 2.Access values by their keys
  • 3.Add and update key-value pairs
  • 4.Loop through a dictionary
๐Ÿ”ค

New words

dictionarykeyvaluepair
๐Ÿ
If you made a dictionary about yourself, what keys would you include? (Hint: name, age, favorite_color...)
๐Ÿค“

Fun Fact!

Python dictionaries are also called "dicts" for short. They are one of the most used data types in Python because they are so fast at looking things up!

๐Ÿ“– What is a Dictionary?

A dictionary stores data in key-value pairs. Instead of accessing items by position (like lists), you access them by name. It is like a real dictionary โ€” you look up a word (key) and find its meaning (value).

โœจ Create a Dictionary

Dictionaries use curly braces {}and each pair is written as key: value.

๐ŸCreate and access a dictionary
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ”„ Add and Update Values

You can add new keys or change existing ones just by assigning a value!

๐ŸAdd and update dictionary entries
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ”„ Loop Through a Dictionary

You can loop through keys, values, or both!

๐ŸLoop through dictionary keys
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ“‹ List vs Dictionary

๐Ÿ“‹ List

Access by position (index)

names[0] โ†’ "Alice"

๐Ÿ“– Dictionary

Access by name (key)

pet["name"] โ†’ "Whiskers"

๐Ÿ
Try this: create a dictionary about yourself with keys for name, age, favorite_color, and favorite_food. Print each one!
๐Ÿง 

Quick Quiz!

+10 XP

How do you access the value for key 'name' in a dictionary called 'person'?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!