Dictionaries
Store and access data using key-value pairs for fast and organized data management.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
A dictionary stores data in key-value pairs . Instead of accessing items by position (like lists), you access them by a unique key name.
There are two ways to access dictionary values:
Dictionaries are mutable — you can add new key-value pairs or update existing ones using the same syntax:
Use in to check if a key exists before accessing it:
Dictionaries can contain other dictionaries (or lists) as values. This is useful for organizing complex data.
Like list comprehension, you can create dictionaries in one line:
Lesson 8 done — you can now model real-world data in Python!
Dictionaries are how Python stores structured data — user profiles, API responses, config settings. You know how to create, access, update, loop, and nest them safely.
🚀 Up next: File Handling — learn to save data permanently so it survives after your program closes.
Practice quiz
How does a dictionary store data?
- By index position
- In key-value pairs
- As a sorted set
- As a fixed tuple
Answer: In key-value pairs. Dictionaries store data as key-value pairs, accessed by key name.
Which brackets create a dictionary?
Dictionaries use curly braces, e.g. {"name": "Alice"}.
What happens when you access a missing key with dict['missing']?
- Returns None
- Returns 0
- Raises a KeyError
- Adds the key
Answer: Raises a KeyError. Square-bracket access on a missing key raises a KeyError.
What does person.get('email', 'N/A') return if 'email' is missing?
- None
- 'N/A'
- KeyError
- Empty string
Answer: 'N/A'. .get() returns the supplied default ('N/A') when the key is absent.
How do you add a new key 'age' with value 25 to dict person?
- person.add('age', 25)
- age
Answer: age. Assigning to a new key, person['age'] = 25, adds the pair.
Which loop gives you both keys and values?
- for k in dict:
- for v in dict.values():
- for k, v in dict.items():
- for i in dict.keys():
Answer: for k, v in dict.items():. .items() yields (key, value) pairs you can unpack in the loop.
Can a list be used as a dictionary key?
- Yes, always
- No, keys must be immutable
- Only if it's empty
- Only with .get()
Answer: No, keys must be immutable. Keys must be immutable (str, int, tuple); a list raises a TypeError.
What does {x: x**2 for x in range(1, 6)} create?
- {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
- {1, 4, 9, 16, 25}
Answer: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}. The dict comprehension maps each x (1-5) to its square.
If a dictionary literal has a duplicate key {'a': 1, 'a': 2}, what is the value of 'a'?
- 1
- 2
- Error
Answer: 2. Keys are unique; the last value wins, so 'a' is 2.
What does len() return for {'name': 'Alice', 'age': 25, 'city': 'London'}?
- 2
- 3
- 6
- 1
Answer: 3. len() counts the number of key-value pairs: 3.