Programming, problem solving, and algorithms

CPSC 203, 2025 W2

January 20, 2026

Announcements

List Activity

Let’s explore some surprising list behaviors.

Open the List Activity on PrairieLearn.

Inside the workspace there are 4 files, each containing a short sequence of exercises. Complete them, and then we’ll review together.

Be curious. Feel free to experiment by changing things!

Write down surprising behaviors.

10:00

Lists: A Quick Review

Lists are ordered collections that can hold any type of data.

We can:

  • Access elements by index: provinces[0]
  • Slice: numbers[1:3]
  • Get length: len(provinces)

Adding to Lists

What about provinces.append(['Ontario', 'Quebec'])?

List Aliasing

Assignment doesn’t copy lists – it creates another reference to the same list.

Shallow Copy

.copy() only copies the outer list. The inner lists are still shared!

Deep Copy

Use deepcopy when you have nested structures (lists of lists, lists of objects).

References

Variables are names that refer to objects in memory.

Think of it like sticky notes on boxes - m1 and m2 are two sticky notes on the same box.

List Comprehensions

A compact way to create lists:

Both produce: [0, 1, 4, 9, 16]

List Comprehensions with Conditions

Notice the difference: if at the end filters, if/else before for transforms.

What you learned

  • Aliasing: assignment creates references, not copies
  • Shallow copy: .copy() only copies the outer structure
  • Deep copy: copy.deepcopy() copies everything
  • List comprehensions: compact syntax for creating lists