What is a dictionary (also called map or hash map)?
Options
- A set of key-value pairs, looked up by key (Correct option)
- An ordered list of numbers, accessed by position
- A text file that contains a list of words
- A sorting algorithm that is used for text
Answer
A collection of key-value pairs where you look up a value by its key, like a phone book
Why
In a phone book you look up a name (key) and find a number (value). A dictionary works the same way.
Python: ages = {"Ana": 30, "Luis": 25} and ages["Ana"] returns 30. Java: Map<String, Integer> (HashMap).
Looking up a key takes about the same time no matter how big the dictionary is (O(1) on average).
Remember
Dictionary = key → value, fast lookup.