Programming Basics
Dictionaries (key-value pairs)
The idea, in plain English
A dictionary is like a phone contacts list. Instead of finding things by a locker NUMBER like a list does, you find them by a NAME, called a 'key'. Look up the key 'phone', and you instantly get back its matching value. You don't need to check every entry one by one.
How it works
- 1Create a dictionary with curly braces. Pair each key to a value with a colon: `{ "name": "Ravi" }`.
- 2Read a value by its key. In JavaScript, use a dot (`student.name`) or brackets (`student["name"]`). In Python, always use brackets (`student["name"]`).
- 3Add a new key any time by assigning a value to it. The dictionary just grows to fit.
When you'd use it
Use a dictionary whenever your data naturally comes in named pairs. Examples: a person's details (name, age, email), app settings, or any 'lookup table' where you want to find things by name instead of position.
Common beginner mistakes
- Looking up a key that doesn't exist. This errors out or gives back nothing useful. Check that the key exists first if you're not sure.
- Confusing a dictionary's keys (the names, like 'age') with a list's indexes (the positions, like 0). They are looked up in completely different ways.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Expected output — hit Run to try it
Name: Ravi
Grade: 8
Passed: yes
Age: 13Not sure this is the right topic? See the learning paths → or where this leads →