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.
Week 07 / Organise 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
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?
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.
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.
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.
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.
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)))Acquisition order: [0, 2, 2, 9, 3] Sorted view: [0, 2, 2, 3, 9] Peak index: 3
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 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?
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.
Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.
raw = [0, 2, 2, 9, 3]; analysis = raw.copy(); sort analysis
Question: Does the acquisition order change?
Two views can answer different questions if their meaning stays explicit.
raw = [0, 2, 2, 9, 3]; analysis = raw; sort analysis
Question: What will printing raw show?
A second variable name does not create an independent record. This is an evidence-preservation bug.
Original [0, 2, 2, 9, 3]; unique values [0, 2, 9, 3]
Question: Does removing the repeated 2 preserve the mean?
Repeated readings are not necessarily duplicate records. You changed the evidence, not just its appearance.
Interactive walkthroughs of Lists Fundamentals. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.
Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.
Something to take away: An unchanged raw list, a labelled derived view, and a demonstration of the aliasing fault with a clear correction.
Read the notebook's teaching cells before these exercises.
Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.
Notes stay in this browser. Download a copy to keep them.
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.