0
0
Intro to Computingfundamentals~6 mins

Dictionaries and key-value pairs in Intro to Computing - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a big box of labeled jars, and you want to find the contents quickly without opening each jar. This is the problem dictionaries solve in computing: storing and finding information fast using labels called keys.
Explanation
What is a Dictionary?
A dictionary is a way to store information where each piece of data has a unique label called a key. Instead of just a list of items, you have pairs: a key and its matching value. This makes it easy to find the value by looking up its key.
A dictionary stores data as pairs of keys and values for quick access.
Keys and Values
Keys are like names or labels that identify each piece of data. Values are the actual data or information linked to those keys. Each key must be unique, but values can repeat or be anything like numbers, words, or even lists.
Keys uniquely identify values, which can be any type of data.
How Dictionaries Work
When you want to find a value, you give the dictionary the key. The dictionary then quickly finds and returns the matching value. This is faster than searching through a list because the dictionary uses the key to jump directly to the data.
Dictionaries use keys to quickly find and return their matching values.
Adding and Changing Data
You can add new key-value pairs to a dictionary or change the value for an existing key. If you add a pair with a key that already exists, the old value is replaced with the new one.
Dictionaries allow adding new pairs and updating values by key.
Real World Analogy

Think of a dictionary like a phone book where each person's name is a key and their phone number is the value. When you want to call someone, you look up their name and find their number quickly.

Dictionary → Phone book containing names and phone numbers
Key → Person's name in the phone book
Value → Person's phone number linked to their name
Adding and Changing Data → Adding a new contact or updating a phone number in the phone book
Diagram
Diagram
┌───────────────┐
│  Dictionary   │
├───────────────┤
│ Key   │ Value │
├───────┼───────┤
│ Name  │ Alice │
│ Age   │  30   │
│ City  │ Paris │
└───────┴───────┘
A simple dictionary table showing keys and their matching values.
Key Facts
DictionaryA data structure that stores data as key-value pairs.
KeyA unique label used to identify a value in a dictionary.
ValueThe data associated with a key in a dictionary.
Unique KeyEach key in a dictionary must be different from all others.
Key LookupThe process of finding a value by using its key.
Common Confusions
Thinking keys can be duplicated in a dictionary.
Thinking keys can be duplicated in a dictionary. Keys must be unique; if you add a pair with an existing key, it replaces the old value.
Believing values must be unique like keys.
Believing values must be unique like keys. Values can be repeated or any type; only keys need to be unique.
Assuming dictionaries keep data in order like lists.
Assuming dictionaries keep data in order like lists. Dictionaries do not guarantee order; they focus on fast key-based access.
Summary
Dictionaries store data as pairs of unique keys and their matching values for fast access.
Keys act like labels to find values quickly, similar to names in a phone book.
You can add new pairs or update existing values by using their keys.