CP1 / COURSE HOME

Week 07 / Organise observations

Keep the original observations

Store a sequence so you can ask more than one question of it.

Last week we selected useful summaries. A summary cannot answer every later question, so this week we retain the readings it came from.

A position sensor records [0, 2, 2, 9, 3] mm in that order. One person wants the largest value; another wants to know what happened immediately afterward.

Start with the problem

Try this first.

Answer both questions from the original sequence. Then sort it on paper and try again. Which answer has become impossible to recover from the sorted values alone?

Why this week's tool?

A list stores the sequence. Indexing retrieves a position, and a separate sorted copy can answer ordering questions without changing the original log.

By the end: Find a maximum while preserving enough information to say when it occurred and what followed it.

The idea behind the program

Order carries information

A list index can represent acquisition order. The value 9 is the fourth reading, at zero-based index 3. Sorting may help a distribution question but removes the original ordering needed to discuss when the spike occurred.

Repeated values can be real observations

Two consecutive 2 mm readings can represent a stationary interval. Removing duplicates changes the sample count and weighting. Deduplication needs a reason tied to record identity, not just repeated numeric values.

A new name is not always a new list

analysis = raw makes two names refer to the same list. Sorting analysis then changes raw too. For this flat list of numbers, raw.copy() creates a separate list suitable for the experiment.

A short Python example

Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.

raw_mm = [0, 2, 2, 9, 3]
analysis_mm = raw_mm.copy()
analysis_mm.sort()
print('Acquisition order:', raw_mm)
print('Sorted view:', analysis_mm)
print('Peak index:', raw_mm.index(max(raw_mm)))

Example output

Acquisition order: [0, 2, 2, 9, 3]
Sorted view: [0, 2, 2, 3, 9]
Peak index: 3

A list is an object, and names can share it

Assignment of a list to another name does not copy its elements into a new list. Both names can refer to one mutable object. A copy creates a new outer list; it does not automatically copy nested objects.

Draw or trace

Draw raw → [3, 1, 3] and view pointing to the same box after view = raw. Draw a second box instead after view = raw.copy().

Predict before running. If view.sort() runs in each version, which version preserves the acquisition order in raw?

Trace and explanation — after your prediction
  1. With an alias, sorting the shared list makes both names show [1, 3, 3].
  2. With a copy of this flat numeric list, raw stays [3, 1, 3] while view becomes [1, 3, 3].
  3. The duplicate 3 is still a separate observation; removing it changes the dataset.

The copied version preserves raw order here. Sorting in place changes a list; it returns None rather than the sorted list. Object identity and displayed values answer different questions.

Change one thing. Replace the elements with nested lists. Explain why a shallow copy can still share an inner list before trying an inner-list mutation.

Türkçe: İki ad aynı listeyi gösterebilir. Kopya dış listeyi ayırır; iç içe nesneler sığ kopyada ortak kalabilir.

Examples and variations

Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.

01Separate view

raw = [0, 2, 2, 9, 3]; analysis = raw.copy(); sort analysis

Question: Does the acquisition order change?

  1. Copy creates a separate flat list
  2. Sort the copy into [0, 2, 2, 3, 9]
  3. Original remains [0, 2, 2, 9, 3]

Original preserved; peak was at index 3

Two views can answer different questions if their meaning stays explicit.

02Shared-list fault

raw = [0, 2, 2, 9, 3]; analysis = raw; sort analysis

Question: What will printing raw show?

  1. Both names refer to one list
  2. sort changes that list in place
  3. raw now displays [0, 2, 2, 3, 9]

Original order lost

A second variable name does not create an independent record. This is an evidence-preservation bug.

03Deduplication fault

Original [0, 2, 2, 9, 3]; unique values [0, 2, 9, 3]

Question: Does removing the repeated 2 preserve the mean?

  1. Original sum = 16; count = 5; mean = 3.2 mm
  2. Unique sum = 14; count = 4; mean = 3.5 mm
  3. An observation was removed without a fault criterion

Mean changes from 3.2 to 3.5 mm

Repeated readings are not necessarily duplicate records. You changed the evidence, not just its appearance.

See the Colab code run

Interactive walkthroughs of Lists Fundamentals. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.

Work on it in Colab

Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.

  1. Annotate each original value with its index. Explain what index 3 means and what it does not tell you without timestamps.
  2. Compare assignment with copy using the supplied flat list. Print both lists before and after sorting.
  3. Calculate the mean before and after removing a repeated value. Propose a situation in which repeated values are expected.
  4. Give a partner both views and ask them to locate the spike in acquisition order. Decide how the report should label derived data.

Something to take away: An unchanged raw list, a labelled derived view, and a demonstration of the aliasing fault with a clear correction.

Suggested exercises, downloads & solutions

Read the notebook's teaching cells before these exercises.

  • EX04 Reverse a List (Medium)
  • EX06 Remove Duplicates (Medium)
  • EX08 List Statistics (Medium)
Download notebookWorked solutions

Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.

Optional notes & guidance
My notes
Using AI or working with a partner

Ask AI to clean a sensor log. Reject automatic sorting or deduplication unless it explains why the operation fits the question. Test whether its function mutates the caller's list.

You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.