──────────────────────────────────────

42. Reliable Automation · Recognise a Duplicate by Its ID

Avoid counting a repeated record while keeping distinct records with equal values.

Lesson files · lessons/reliable-automation/repeatable-data

Before you start
- Check membership in a set and add new entries.
- Keep a separate counter while looping over records.

By the end
- Count distinct event IDs and repeated occurrences.
- Explain why two records with the same quantity can belong to different events.

Where you can use it
Merge a second event export by recognising the same event ID and retaining other events with equal counts.

Two valid sightings can have the same animal count. Check duplication by the field that identifies the record. Keep a set of IDs and update it only after a record has passed validation.

Predict
Why does the second r1 get skipped?
- The second line was previously skipped.
- It has the same ID as an earlier record.

Worked example (example.py)

seen = set()
for code in ["r1", "r2", "r1"]:
    if code not in seen:
        seen.add(code)
        print(code)


Example result
r1
r2

Read three event IDs. Print Accepted: followed by the number of distinct IDs and Duplicates: followed by how many repeats were seen.

Starter program (starter.py)

seen = set()
for code in ["r1", "r2", "r1"]:
    if code not in seen:
        seen.add(code)
        print(code)


Hint
Count one duplicate for each ID found in seen. Use len(seen) to count accepted records.

Case 1. Try the sample
Input
a1
b7
a1
Expected output
Accepted: 2
Duplicates: 1

Case 2. Three copies
Input
x
x
x
Expected output
Accepted: 1
Duplicates: 2

Stretch
Add handling for a missing ID. Explain why missing and duplicate are different.

Reflect
Which field would make a poor identifier in a reading log, and why?

Check your prediction
It has the same ID as an earlier record. The ID is already in the set.
