Concepts / Creating and Initializing Dictionaries

Creating and Initializing Dictionaries

A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.

  • Programming

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

positionpositionkeykeyList0namenameperson's name1ageDictionaryageperson's age
How does a list use a numeric position while a dictionary uses a descriptive key?

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.

containscontainscontainsmaps tocontainsmaps toDictionaryKey-value pairemailkeyagekeyKey-value pairemail valuevalueage valuevalue
What does one key-value pair contain, and how do multiple pairs combine into a dictionary?

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.

Choose the keys: Use student_id and grade as descriptive keys because they identify what each value represents.

Attach one value to each key: The student_id key maps to the student's identifier, and the grade key maps to the student's grade.

Treat 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.

maps tomaps toEnglish wordkeySpanish equivalentvalueEnglish wordkeySpanish equivalentvalue
Which value is associated with each key, and how does a key lead to its corresponding 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

identified byidentified byList datanumeric positionsIndex 2gradeDictionary datadescriptive keysgradestudent's grade
How can descriptive keys identify values when there is no meaningful numeric position?

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.

Identify the information: The record needs four kinds of information: name, age, email, and phone number.

Turn each description into a key: Use name, age, email, and phone number as meaningful labels for the information.

Pair each key with one value: Each label maps to the corresponding information for the person. Every label-value combination is one key-value pair.

Combine 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

  • Treating a dictionary like a list and assuming every lookup must use an integer position.

    A dictionary is organized around keys, and its keys can be strings, numbers, tuples, or other types.

    Fix: Use a meaningful key such as grade when the label itself explains the value.

  • Confusing a key with its value.

    The key is the label used for lookup; the value is the information associated with that label.

    Fix: Describe each entry as a key mapped to one value.

  • Assuming a key can identify several values at the same time.

    Each key maps to exactly one value.

    Fix: Use separate key-value pairs for separate pieces of information.

  • Choosing arbitrary numeric positions when the data has meaningful labels.

    The positions do not explain what the values represent, making the data less readable.

    Fix: Use descriptive keys such as student_id and grade.

Check Your Understanding

EASY

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?

  • The value associated with email
  • The numeric position of email
  • Every value in the dictionary
Reveal answer

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

  1. A dictionary is a mapping between keys and values, not a sequence indexed by position like a list.
  2. A key-value pair contains a key used for lookup and one associated value.
  3. Each key maps to exactly one value.
  4. Dictionary keys can be almost any type, including strings, numbers, and tuples, while list indices must be integers.
  5. Meaningful keys make data easier to understand and allow direct, intuitive lookups.

Key Takeaways

  • 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.