Accessing Dictionary Values
The central distinction is simple: a list lookup supplies a numeric position, while a dictionary lookup supplies a key.
From Positions to Labels
Suppose you need to store information about a person: their name, age, email, and phone number. A list could hold these pieces of information, but you would need to remember that position 0 contains the name, position 1 contains the age, and so on. Anyone reading the data would have to memorize what each number means.
A dictionary solves this organization problem by using meaningful labels called keys. Instead of remembering that position 0 contains a name, you can associate the value with the key name. To retrieve the value, you provide the label that identifies what you want.
Two Access Systems
A list is a sequence accessed by position. Its indices must be integers, so an expression such as my_list[0] asks for the item at the first position. A dictionary is a mapping accessed by key. An expression such as my_dict['name'] asks for the value associated with the key name. The key directly identifies the requested data instead of making you count positions.
The central distinction is simple: a list lookup supplies a numeric position, while a dictionary lookup supplies a key.
Following a Key
Think of a dictionary as a collection of links from keys to values. When you provide a key, the dictionary follows the mapping and returns the value connected to that key. The dictionary does not treat the key as a position to count toward. It uses the key itself to locate the associated value.
Key-Value Pairs
A key-value pair is the fundamental unit of a dictionary. It consists of a key and the one value associated with that key. For example, the key name can identify a person's name, while the key email can identify that person's email address.
The phone-book analogy makes this relationship clear. A person's name acts as the key, and the person's phone number acts as the value. You use the name to look up the phone number. The dictionary stores that key and value as one connected relationship.
A Translation Lookup
English Words to Spanish Equivalents
A translation tool maps English words to their Spanish equivalents. What happens when the tool looks up an English word?
- 1Identify the key
The English word being searched for is the key.
- 2Follow the mapping
The dictionary uses that key to locate the associated Spanish word.
- 3Return the value
The Spanish equivalent connected to the English key is the value returned by the lookup.
The English word identifies the entry, and the associated Spanish word is retrieved as its value.
This example demonstrates why a dictionary is more natural than a list for the task. A list would require a position and would not make the English-to-Spanish relationship visible. A dictionary makes the relationship explicit: each English word is a key, and each Spanish word is the value mapped to it.
Why Non-Integer Keys Help
Dictionary keys can be almost any type, including strings, numbers, and tuples. This differs from list indices, which must be integers. Because a dictionary key can describe what a value represents, the data becomes easier to read and retrieve. Examples include using name, age, and email for person information or student_id and grade for student information.
Meaningful keys also avoid a specific problem with lists: finding information when you do not know its position. With a list, you may need to search through the entire list to find an email. With a dictionary, the email key directly identifies the value to retrieve. The source material describes this direct lookup as more readable and faster than searching by position.
Mistakes to Avoid
Common mistake Treating a dictionary like a list
What it looks likeThinking that the first dictionary value must be retrieved with the position 0.
Why it failsA dictionary is not a sequence indexed by position. Its lookup uses a key.
Do this insteadIdentify the key that labels the value you want, such as name or email.
Common mistake Confusing a key with its value
What it looks likeCalling email the person's email address rather than the label used to retrieve it.
Why it failsIn a key-value pair, email is the key and the associated email address is the value.
Do this insteadSeparate the lookup label from the data returned by that lookup.
Common mistake Assuming every key must be an integer
What it looks likeBelieving that dictionary access can use only numbers.
Why it failsDictionary keys can be almost any type, including strings, numbers, and tuples.
Do this insteadChoose a key that meaningfully identifies the value.
Common mistake Forgetting that a key maps to one value
What it looks likeTreating one key as if it independently identifies several unrelated values.
Why it failsEach key maps to exactly one value in the dictionary relationship described here.
Do this insteadThink of each key and its associated value as one key-value pair.
Practice the Lookup
A student record contains an identifier, a name, and a grade. Explain why the keys student_id and grade are clearer lookup labels than positions 0 and 2. Then describe the two parts of the key-value pair formed by grade.
Hints
- Ask whether a reader must memorize what position 0 or position 2 means.
- Separate the label used for lookup from the data associated with that label.
What do you think happens?
A dictionary stores a person's name under the key name. If you want the person's email, should you provide the position where the email happens to appear or the key email?
Reveal answer
The key email
Dictionary lookup uses the key directly. It does not require you to count positions as a list lookup does.
Key Ideas
- 1A list is accessed by integer position, while a dictionary is accessed by key.
- 2A dictionary is a mapping between keys and values rather than a sequence indexed by position.
- 3A key-value pair is the fundamental unit of a dictionary: one key maps to exactly one value.
- 4Keys can be almost any type, including strings, numbers, and tuples, so they can serve as meaningful labels.
- 5Direct key lookup makes data retrieval more readable and faster than searching through a list by position.
Key Takeaways
- Lists use integer positions; dictionaries use keys.
- A dictionary maps each key to exactly one associated value.
- The key-value pair is the basic unit of dictionary organization.
- Meaningful keys such as name, email, or grade make data easier to understand and retrieve.
- Dictionary lookup directly identifies the desired value instead of requiring a search by position.