Data Structures · Unit 3

Lists

Ordered, changeable collections

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

Ordered · changeable · duplicates allowed

Lists: a numbered row of values

A list keeps its items in order. Each position has an index starting at 0, so the first item is at index 0 and the last item is at index -1. Lists can grow, shrink, and be edited while a program runs.

Think of a supply tray: each compartment has a position, and you can add, replace, or remove an item without rebuilding the tray.

List operations lab

Choose an operation, enter an item, then watch how the indexed row changes.

index 0Notebook
index 1Pencil
index 2Ruler

Choose an operation to change the list. Length: 3

append(x)Add one item at the end.

insert(i, x)Place an item at a chosen index.

remove(x)Remove the first matching value.

Run the list operation in Python
Starting up Python…
supplies = ["Notebook","Pencil","Ruler"]
supplies.append("Eraser")
print("Items:", supplies)
print("Count:", len(supplies))
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 list mistakes

  • IndexError: an index must exist; for three items, valid positive indexes are 0, 1, and 2.
  • remove() versus pop(): remove a value with remove(value); remove a position and receive its value with pop(index).
  • Duplicates are allowed: adding an existing value does not automatically remove the earlier one.
Lists check

1. What is the index of the first list item?

2. Which method adds one item to the end of a list?

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.