Intermediate Topic 20 of 54 Programming Fundamentals
Dictionaries and Maps
Key-value collections offering fast lookup by key.
How it works
A map stores key-value pairs and finds a value by hashing the key to a bucket, giving average O(1) lookup, insert and delete. Keys must be unique and hashable, and collisions are resolved by chaining or probing. Some implementations keep insertion order, while tree-based maps keep keys sorted at the cost of O(log n) operations.
Step-by-step walkthrough
- You look up the key "age".
- The key is hashed to a number.
- The number picks a bucket.
- The bucket holds the value 30, so lookup is fast on average.
- Keys must be unique and hashable.
Where it is used
Word counts, caches, configuration, indexing records by ID, grouping data and counting occurrences are everyday uses. Maps turn many nested-loop searches into single lookups.
Common pitfall
Iteration order may be undefined in some languages, and using mutable objects as keys breaks lookup after they change. Reading a missing key can raise an error or return null, so use get with a default. Poor hash functions or adversarial keys can degrade performance.
Where it fits in the course
Dictionaries and Maps is topic 20 of 54 in Programming Fundamentals, rated Intermediate. It follows Strings. The next topic is Sets.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Dictionaries and Maps?
Where do people use Dictionaries and Maps?
What is the most common mistake with Dictionaries and Maps?
What should I learn before this topic?
How can I practice it?
Is Dictionaries and Maps hard to learn?
How long does it take to learn Dictionaries and Maps?
Which topics are related to Dictionaries and Maps?
What should I study after Dictionaries and Maps?
Related topics
- Strings (Programming Fundamentals)
- Sets (Programming Fundamentals)
- Arrays and Lists (Programming Fundamentals)
- Tuples (Programming Fundamentals)
- Karnaugh Maps (Computer Architecture & Logic)