SECTION 06 / 24

Compare saved lists: overlap and difference

2 min read
On this page 1 headings

After building the two ID lists, we can find their common members without scanning every original field again. For each May ID x, compare it with each Chennai ID y. On equality, append x to the result.

The common IDs are [3,25], identifying Rida and Geeta. The nested scan compares identifiers, not complete cards.
The common IDs are [3,25], identifying Rida and Geeta. The nested scan compares identifiers, not complete cards.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Find May ∩ Chennai from the saved lists

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.

Common = []
foreach x in MayIDs:
    foreach y in ChennaiIDs:
        if x == y:
            Common = Common ++ [x]

There are 4 × 8 = 32 equality tests in this complete nested scan. We do not divide by two: these are comparisons across two lists, not unordered pairs within one list. Because each input here has unique IDs, the result contains each common student once.

With duplicates, raw nested matching can repeat results: [3,3] matched with [3,3] appends 3 four times. To implement set-style intersection, require unique inputs or add “if NOT member(Common,x)” before appending. Specify which meaning you want.

Find May students who are not in Chennai

REAL COURSE DATA · BUILD AND UNPACK LISTS

Find IDs in one list but not the other

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.

MayOnly = []
foreach x in MayIDs:
    if NOT member(ChennaiIDs, x):
        MayOnly = MayOnly ++ [x]
# [13,23]: Gopi and Deepika

Absence requires checking the whole other list unless a match is found first. Appending x whenever x != y is wrong: almost every ID differs from some y, including IDs that are also present. Union is another set-like operation: begin with one unique list, then append only members of the other list not already present.

Try it yourself

What are May ∩ Chennai and May minus Chennai?

Show explained answer

Intersection: [3,25]. May-only: [13,23], in the original May-list order.

Try it yourself

Why is IF x != y inside the nested loop not a correct set difference?

Show explained answer

A single non-match does not prove x absent. Search all y values and append x only when no match exists.

Try it yourself

Can a list intersection reorder the result?

Show explained answer

Yes, its sequence depends on the traversal method. Membership may be the same even when output order differs. State the intended convention.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑