Introduction to Dictionaries
Hi there! Welcome to one of the most useful sections of the Fundamentals of Data Structures. So far, you’ve probably used Arrays or Lists where you find items using a number (an index). But what if you wanted to find data using a name or a label instead? That is exactly where Dictionaries come in!
Think of a dictionary as a super-powered contact list on your phone. You don't look for your best friend by remembering they are "Contact #42"; you look for them by their name. Dictionaries make searching for specific information fast and intuitive. Let’s dive in!
1. What is a Dictionary?
In Computer Science, a Dictionary is an Abstract Data Type (ADT) that stores data in Key-Value Pairs.
Every single item in a dictionary consists of two parts:
1. The Key: This acts as the unique identifier (like a word in a physical dictionary or a person's name in a phonebook).
2. The Value: This is the actual data associated with that key (like the definition of the word or the person's phone number).
The Golden Rule of Keys
In any dictionary, the Keys must be unique. You can't have two identical keys because the computer wouldn't know which value to give you back! However, the Values do not have to be unique—two different keys can point to the same value.
Quick Review: Key vs. Value
- Key: What you use to look something up (Must be unique).
- Value: What is returned to you (Can be anything).
Key Takeaway
A dictionary is a collection of key-value pairs where you access a value by using its associated key.
2. Dictionaries vs. Arrays
Don't worry if this seems a bit abstract! Let's compare it to something you already know: the Array.
In an Array, the "key" is always an integer index (0, 1, 2...). The computer decides the position.
In a Dictionary, the "key" can be almost any data type, like a string. You (the programmer) decide what the label is.
Array: Names[0] = "Alice"
Dictionary: Ages["Alice"] = 17
Did you know?
Dictionaries are often much faster for finding specific items than arrays. In a massive array, you might have to check every single box to find "Alice." In a dictionary, you just ask for "Alice," and the computer knows exactly where the value is stored!
3. Real-World Applications
The AQA syllabus specifically mentions Information Retrieval as a core use for dictionaries. A classic example is counting how many times words appear in a document.
Step-by-Step: Word Frequency Counting
Imagine we have the sentence: "The green, green grass grows". We want to store how many times each word appears. We can represent this as a dictionary:
1. "The": appears 1 time.
2. "green": appears 2 times.
3. "grass": appears 1 time.
4. "grows": appears 1 time.
In programming notation, it would look like this:
{ 'the': 1, 'green': 2, 'grass': 1, 'grows': 1 }
Key Takeaway
Dictionaries are perfect for retrieving information quickly, such as looking up definitions, IDs, or counting frequencies.
4. Representing Vectors as Dictionaries
Later in the course, you will look at Vectors. The syllabus notes that a dictionary is a very useful way to represent a vector when we view it as a function.
For a vector like \( [2.0, 3.14, -1.0] \), we can think of it as a mapping where the index maps to a value:
\( 0 \mapsto 2.0 \)
\( 1 \mapsto 3.14 \)
\( 2 \mapsto -1.0 \)
In a dictionary, this would be: {0: 2.0, 1: 3.14, 2: -1.0}. This is especially helpful for "sparse" vectors where most of the values are zero, as we only need to store the keys that actually have data!
5. Common Operations
When working with dictionaries in your programming projects, you will usually perform these four tasks:
- Add a new key-value pair.
- Delete an existing key-value pair.
- Amend (change) the value associated with an existing key.
- Lookup the value associated with a key.
Common Mistake to Avoid:
Trying to access a key that doesn't exist! If you ask a dictionary for the value of "Bob" but "Bob" isn't in there, your program will likely crash with a "Key Error." Always check if the key exists first!
Memory Aid: The Keychain
Think of a physical keychain. Each key has a unique shape (The Key). When you use that key, it opens one specific door (The Value). You can have many keys on the chain, but each one is different!
Quick Review Summary
What is it? A collection of Key-Value pairs.
Is it ordered? Conceptually, dictionaries are unordered. We care about the association, not the sequence.
Key rules: Keys must be unique; Values don't have to be.
Main use: Fast information retrieval and representing complex mappings (like vectors).
Final Encouragement
Dictionaries might feel a bit strange if you are used to the strict "0, 1, 2" indexing of arrays, but once you start using them, you'll find they make your code much easier to read and much faster to run. Keep practicing with them in your coding sessions!