Skip to content
BENIBIX Learn

Dictionary

Development🧩 Programming ConceptsData and Variables

Also known as Map, Hash Map, Associative Array, dict

A collection of key and value pairs. You look a value up by its key.

Think of it like

Think of it like a coat check. You hand over a ticket, and you get back your own coat.

Example

A class app keeps ages as {"Emma": 36, "Jack": 12}. Asking for "Jack" gives back 12 at once.

Why it matters

Settings, user records and counts are all looked up by name. A dictionary finds them without searching the whole list.

Definition

A dictionary is a collection that stores pairs: a key and a value. You look a value up by its key, not by a number. Each key appears only once, like each word in a real dictionary. Python calls it a dict, JavaScript uses an object or a Map, and many others call it a map. It is the fastest way to find one thing by its name.

Example code

python
>>> ages = {"Emma": 36, "Jack": 12}
>>> ages["Jack"]
12
>>> ages["Bo"]
KeyError: 'Bo'
Look up by name. A name that is not there gives an error.

Common mistake

Asking for a key that is not there and not checking first. Most languages give an error or a null.

Key takeaways

  1. 1
    Key and value. Every entry is a pair.
  2. 2
    Keys are unique. Each key can appear only once.
  3. 3
    Fast lookup. Find by name, not by position.

Compare

Learn it properly