Creating and Initializing Dictionaries
Creating and initializing a dictionary means establishing a collection of key-value pairs.
From Positions to Labels
Imagine storing a person's name, age, email, and phone number in a list. The list can hold the information, but you must remember which numeric position contains each item. The name might be at position 0, the age at position 1, and the email at another position. That arrangement makes the data harder to read because the position itself does not explain what the value means.
A dictionary organizes the same kind of information through meaningful labels called keys. Instead of asking for the item at a numeric position, you use a key that describes the data, such as name, age, or email. This is the central difference between a list and a dictionary: a list is indexed by position, while a dictionary is a mapping between keys and values.
Two Kinds of Lookup
A list lookup begins with a number representing a position. A dictionary lookup begins with a key representing the data you want. The list's position tells you where an item is located in the sequence. The dictionary's key tells you what the associated value represents.
A dictionary is not simply a list with a different appearance. It uses a different organizing idea: keys identify values directly instead of numeric positions identifying items by location.
Key-Value Pairs
A key-value pair is the fundamental unit of a dictionary. The key is the label used for lookup, and the value is the information associated with that key. Each key maps to exactly one value.
Organizing a Student Record
Organize a student's identifier and grade so that each value can be found through a meaningful label rather than a remembered list position.
- 1Choose the keys
Use
student_idand grade as descriptive keys because they identify what each value represents. - 2Attach one value to each key
The
student_idkey maps to the student's identifier, and the grade key maps to the student's grade. - 3Treat each entry as a pair
Each label and its associated information form one key-value pair. Together, the pairs form the dictionary.
The record is organized around meaningful labels, so a reader does not need to remember which numeric position represents the identifier or the grade.
Following a Lookup
Consider a translation tool that maps English words to their Spanish equivalents. The English word is the key, and the Spanish word is the value. When a learner supplies an English key, the dictionary follows that mapping and returns the associated Spanish value.
The important operation is the direction of the lookup: start with a key and retrieve its associated value. This differs from a list, where the lookup starts with a position number. The dictionary's internal organization is designed to locate the mapped value almost instantly, regardless of how many key-value pairs it contains.
Meaningful Non-Integer Keys
Lists require integer indices because their items are organized sequentially. Dictionaries use a different internal organization, so their keys can be almost any type, including strings, numbers, and tuples. This lets a key describe the value instead of merely describing where the value appears in a sequence.
This flexibility is useful when a numeric position has no natural meaning. A student's grade is better identified by a key such as grade than by a statement that it happens to be at position 2. A person's email is better identified by email than by a position that another reader must memorize.
When the identity of the data matters more than its position, organize the data with descriptive dictionary keys. Meaningful keys make the structure easier to read and make the intended lookup more intuitive.
Initialization as Assembly
Creating and initializing a dictionary means establishing a collection of key-value pairs. Each pair contributes one mapping: a chosen key identifies one associated value. A dictionary can therefore be understood as being assembled entry by entry, with the complete collection representing one organized record or mapping.
Building a Person's Information Record
Plan a dictionary for a person's name, age, email, and phone number.
- 1Identify the information
The record needs four kinds of information: name, age, email, and phone number.
- 2Turn each description into a key
Use name, age, email, and phone number as meaningful labels for the information.
- 3Pair each key with one value
Each label maps to the corresponding information for the person. Every label-value combination is one key-value pair.
- 4Combine the pairs
The four pairs together form one dictionary that can be read by meaning rather than by remembered numeric positions.
The initialized structure is a mapping of descriptive keys to the person's associated values.
Common Misunderstandings
Common mistake Treating a dictionary like a list and assuming every lookup must use an integer position.
What it looks likeThinking of the grade only as the item at position 2.
Why it failsA dictionary is organized around keys, and its keys can be strings, numbers, tuples, or other types.
Do this insteadUse a meaningful key such as grade when the label itself explains the value.
Common mistake Confusing a key with its value.
What it looks likeTreating email as though it were the person's actual email information.
Why it failsThe key is the label used for lookup; the value is the information associated with that label.
Do this insteadDescribe each entry as a key mapped to one value.
Common mistake Assuming a key can identify several values at the same time.
What it looks likeExpecting one key to independently map to an age and an email.
Why it failsEach key maps to exactly one value.
Do this insteadUse separate key-value pairs for separate pieces of information.
Common mistake Choosing arbitrary numeric positions when the data has meaningful labels.
What it looks likeRequiring readers to remember that position 0 means student ID and position 2 means grade.
Why it failsThe positions do not explain what the values represent, making the data less readable.
Do this insteadUse descriptive keys such as student_id and grade.
Check Your Understanding
A record stores a student's identifier, name, and grade. Decide whether each piece of information should be identified by a numeric list position or by a descriptive dictionary key. Then explain why the dictionary approach may be clearer.
Hints
- Ask whether the position itself has meaning.
- Name a key that directly describes each value.
- Remember that each key maps to exactly one value.
What do you think happens?
A dictionary contains a key labeled email. What does that key allow you to retrieve?
Reveal answer
The value associated with email
A dictionary lookup uses the key to locate its associated value. The key is the label, not a numeric position and not the complete collection.
Key Takeaways
- 1A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.
- 2A key-value pair contains a key used for lookup and one associated value.
- 3Each key maps to exactly one value.
- 4Dictionary keys can be almost any type, including strings, numbers, and tuples, while list indices must be integers.
- 5Meaningful keys make data easier to understand and allow direct, intuitive lookups.
At a glance
- Lists locate values by numeric position; dictionaries locate values by key.
- The key-value pair is the basic building block of a dictionary.
- Each key maps to exactly one associated value.
- Descriptive keys organize information when numeric positions are not meaningful.
- Initializing a dictionary means assembling related key-value pairs into one mapping.