Data Structures · Unit 3

Dictionaries

Key → value pairs

Unit 3 · Data Structures

Choose the right container for your data

A data structure organizes values so a program can find and work with them. Explore four built-in Python structures and use each lab to see how its rules affect real data.

Lists

Ordered and changeable

Tuples

Ordered and fixed

Sets

Unique values

Dictionaries

Key → value lookup

Keyed lookup · changeable · insertion order preserved

Dictionaries: look up a value by its key

A dictionary maps each unique key to a value. Instead of remembering that the course is at position 1, a program can ask for record["course"]. Keys must be unique and hashable.

Think of a student record: the field name is the key, and the stored detail is its value. A label such as “semester” is clearer than an unexplained number in a sequence.

Key–value record lab

Search a field, change its value, or remove it. The record below updates immediately.

studentMira
coursePython
semester2
Enter the field name to search in the record.

Look up a key, update its value, or remove the pair.

record[key]Get a value; missing keys raise KeyError.

record.get(key)Look up safely; returns None if missing.

record[key] = valueAdd a key or replace its value.

Run the dictionary operation in Python
Starting up Python…
student_record = {
    "student": "Mira",
    "course": "Python",
    "semester": "2"
}
print("Value:", student_record.get("course", "Key not found"))
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Avoid common dictionary mistakes

  • A missing key can raise KeyError: use get() when the key may not exist.
  • Keys are unique: assigning to an existing key replaces its old value; it does not create a second pair.
  • Use square brackets for lookup: record["student"] retrieves a value; braces create the dictionary.
Dictionaries check

1. How do you retrieve the value stored under the key 'course'?

2. What does assigning a value to an existing dictionary key do?

Compare the four structures

Ask two questions: does order matter, and should the collection change after creation?

StructureOrdered?Changeable?Access values by
listYesYesPosition (index)
tupleYesNoPosition (index)
setNo guaranteed orderYesMembership, not index
dictInsertion order is preservedYesKey

Use a list for a sequence that changes, a tuple for a fixed record, a set for unique membership, and a dictionary for named lookups.