What is a key-value pair
With arrays, we took values out by position number (Lesson 8). But sometimes you want to find something by name, not by number. Just as you'd look up a friend's phone number by "Minsu," not "the 47th person," let's see how to take a value out directly by its label.
A label instead of a number
The array in Lesson 8 was a row of boxes, and we took values out by position number: 1, 2, 3...
But when you look up a word in a dictionary, you don't think "word number 325." You just turn to "apple" and the meaning is there.
Keys and values work like that: you find things by label.
Pick a name and its value appears right away, without knowing any position number.
There's no need to count.
Know the name, and you go straight to its value.
So how exactly are these labels and their contents paired up?
Pairing up: keys and values
Each slot is always a pair: a label (the key) and what's inside it (the value).
For example, in a Spanish dictionary, "manzana" (key) → 'apple' (value), or "Minsu" (key) → '010-1234' (value).
Call the key, and the value paired with it comes along.
Connect a key to a value and the two are tied into one pair: "key → value."
The key is the name that says where to look, and the value is what's actually stored.
A big collection of these pairs is a dictionary.
But once there are many pairs, how do you find the one you want quickly?
No standing in line
Even with a thousand values, keys and values don't scan from the beginning one by one.
They look at the label and go straight to that slot.
In an array, if you don't know the position you have to search from number 1, but with a key you arrive in one step. That's the secret to the speed.
The left side searches one by one; the right side arrives in one step by label. Press search to see the difference.
If you know the name, there's no long way around.
That's why keys and values are so good at pulling one thing out of many, fast.
Finally, there's one rule labels must always follow.
Labels don't repeat
The rule is simple: each label can be used only once.
You can add as many new labels as you like, but if you put a new value under a label that already exists, the old value is erased and replaced with the new one.
A new label adds a slot; putting in the same label again replaces the old value with the new one.
Because labels never repeat, calling "Minsu" can't be confusing.
You always get exactly one value.
This one simple pair is hiding just about everywhere.
Pairs everywhere, and beyond
Dictionaries (word→meaning), settings (option→value), contacts (name→number), a game inventory (item→count): they're all keys and values.
It's one of the most common ways to organize information.
In the next lesson, we'll move on to bundling labels like these into one chunk and handling it like a single thing.
Tap the contacts, settings, and bag tabs one after another and you'll see all three are the same "key → value."
After arrays in a row (Lesson 8), you now have keys and values you look up by label.
You can handle information by number and by name.
Next come objects, which put several labels into one box and bundle them into a single thing.