All lessons

Dictionaries

8 min · Python

Store values under their own names instead of numbered positions.

Python · Dictionaries0/5 cleared

Enemy 1 of 5

A list is great when order matters and you look things up by numeric position, but a lot of real data is more naturally described by name than by position. A dictionary (sometimes called a map or hash map in other languages) holds key-value pairs inside curly braces: each value is looked up by a meaningful name, its key, instead of a numbered index like a list uses.

Think of a dictionary as a real-world dictionary or a phone book: you don't look up a word by "the 4,281st word in the book," you look it up by the word itself. Similarly, player["name"] reads as "give me the value stored under the key 'name'" rather than "give me item number 0."

Run this to make a dictionary describing a player and print a value by its key.

You
Goblin Glitch
Loading editor…
Target output: Ada

Stuck? Ask Rena

Your AI tutor that gives assistance.

Asking Rena in lessons is on Ultimate.