SECTION 17 / 24

Keep an ID beside a property: lists of pairs

1 min read
[3,53] stores Rida’s CardNo and Physics mark. The first component is an identifier; the last is the chosen property.
[3,53] stores Rida’s CardNo and Physics mark. The first component is an identifier; the last is the chosen property.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Keep IDs beside marks

Follow one occurrence at a time. Choose another input to test empty lists, repeated IDs and different parameters.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change, then step through the list. The original course records remain unchanged.

A list of marks alone loses the owners of those marks. A pair such as [3,53] keeps one identifier beside one property. A list of pairs keeps that connection for every selected record: [[0,64],[1,45],[2,54],…] for Physics in the original dataset order.

Pairs = []
foreach X in ScoreRecords:
    Entry = [X.CardNo, X.Physics]
    Pairs = Pairs ++ [Entry]

foreach Pair in Pairs:
    ID = first(Pair)
    Mark = last(Pair)

A pair is a list of exactly two components. Its order has a schema: [ID,mark] differs from [mark,ID]. first(Pair) is the identifier and last(Pair) the mark only because we agreed on this schema. A pair can also hold an ID and a city string; components need not all be numeric.

This is a projection: keep relevant fields and leave out the rest. The resulting list is enough for tasks about the retained property and IDs, but not for questions about fields you omitted. Keep the original dataset available if later tasks may need birthdays, cities or other subjects.

Try it yourself

Why is [3,53] not the same as [[3,53]]?

Show explained answer

The first is a two-element pair. The second is a one-element outer list whose element is that pair.

Try it yourself

What information does [3,53] lose compared with the full score card?

Show explained answer

It omits fields such as Name, birthday, city, Maths, Chemistry and Total. The ID can reconnect to them only if the source lookup remains available.

Try it yourself

How long is a 30-student list of [ID,Physics] pairs?

Show explained answer

The outer length is 30. Each pair has length 2.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑