- Dictionary stores data in key : value pairs
- Keys must be unique and immutable
- Values can be of any data type
- Dictionaries are mutable
- Very fast lookup using hashing
Syntax Example:
student = {
"name": "Poorna",
"age": 21,
"branch": "CSE"
}
Quiz – Python Dictionary
Click an option to see the answer instantly.
1. What does a dictionary store?
Dictionary stores data in key–value format.
2. Which brackets are used for dictionary?
Curly brackets are used for dictionaries.
3. Are dictionary keys mutable?
Keys must be immutable.
4. Which can be a dictionary key?
Tuples are immutable, so they can be keys.
5. How do you access a value in dictionary?
Dictionary values are accessed using keys.
6. Which method is safest to access value?
get() avoids errors if key is missing.
7. What happens if same key is repeated?
Last value replaces previous one.
8. Which method returns all keys?
keys() returns all keys.
9. Which method returns key and value together?
items() gives key–value pairs.
10. Dictionary lookup time complexity is?
Dictionary uses hashing for O(1) lookup.
- Dictionaries are implemented using hash tables
- They are heavily used in DSA for frequency counting
- Most APIs return data as dictionaries (JSON)
- Backend frameworks rely on dictionaries internally
# Frequency counting example
text = "banana"
freq = {}
for ch in text:
freq[ch] = freq.get(ch, 0) + 1
Advanced Thinking:
Most real-world programs are just dictionaries,
lists of dictionaries, or dictionaries inside dictionaries.
If you understand dictionaries deeply, you are already thinking like a backend and DSA developer.
0 Comments