Day 13 said writes aren't idempotent by default. Today's question: how do you make a write-loop idempotent? The simplest answer — track what you've already done.
seen = set()
items = ["a", "b", "c", "a", "d", "b"]
for item in items:
if item in seen:
continue # already processed — skip
seen.add(item)
# ... do work for `item` here ...Four unique items processed; two duplicates skipped. The set is the dedup key store — its membership tells you what's already done.
A set? That's new.
A built-in container — like a list, but: unordered, no duplicates, fast in check. set() for empty, {1, 2, 3} for literal. Day-to-day, you reach for sets when membership matters more than order.
And continue — skips the rest of the loop body and moves to the next iteration?
Exactly. continue is the loop-body's early-out; analogous to return in a function. Pairs naturally with if condition: continue — the "skip this one" pattern.
Once the script ends, the seen set vanishes. So this only dedupes within one run?
Right — exactly the gap. Surviving a script restart needs persistent state (write seen to a file or a Sheet, read it back on next run). That's Auto Intermediate. For week 2, the in-memory dedup pattern is enough.
setA set is a collection that:
in and add operationsseen = set() # empty
seen = {1, 2, 3} # literal
seen.add(4) # add — duplicates silently no-op
seen.add(2) # already there — no error, no double
print(2 in seen) # True
print(len(seen)) # 4Three operations cover 90% of dedup usage: set(), .add(x), x in s.
seen = set()
for item in items:
if item in seen:
continue # skip duplicate
seen.add(item) # mark as done
process(item) # do the actual workThree-line guard; once you've internalized the pattern you'll write it without thinking.
continue and break| Statement | Effect |
|---|---|
continue | Skip rest of this iteration, go to next |
break | Exit the loop entirely |
Use continue for "skip this one". Use break for "stop now, we found what we wanted / hit a fatal case".
The in-memory seen set vanishes when the script ends. For automations that re-run on a schedule:
seen = set(sheet_read_column()).sheet_append_row([item]) plus seen.add(item).Now re-runs see the same set as last time — true cross-run idempotency. The mechanism (read state, mutate state, write state back) is Auto Intermediate.
A set throws away duplicates and throws away order. If you need both — "unique items in order they first appeared" — you'd use a different structure (a dict keyed on the value, or a list with a parallel set as a guard). Pick the container that matches your need.
Day 13 said writes aren't idempotent by default. Today's question: how do you make a write-loop idempotent? The simplest answer — track what you've already done.
seen = set()
items = ["a", "b", "c", "a", "d", "b"]
for item in items:
if item in seen:
continue # already processed — skip
seen.add(item)
# ... do work for `item` here ...Four unique items processed; two duplicates skipped. The set is the dedup key store — its membership tells you what's already done.
A set? That's new.
A built-in container — like a list, but: unordered, no duplicates, fast in check. set() for empty, {1, 2, 3} for literal. Day-to-day, you reach for sets when membership matters more than order.
And continue — skips the rest of the loop body and moves to the next iteration?
Exactly. continue is the loop-body's early-out; analogous to return in a function. Pairs naturally with if condition: continue — the "skip this one" pattern.
Once the script ends, the seen set vanishes. So this only dedupes within one run?
Right — exactly the gap. Surviving a script restart needs persistent state (write seen to a file or a Sheet, read it back on next run). That's Auto Intermediate. For week 2, the in-memory dedup pattern is enough.
setA set is a collection that:
in and add operationsseen = set() # empty
seen = {1, 2, 3} # literal
seen.add(4) # add — duplicates silently no-op
seen.add(2) # already there — no error, no double
print(2 in seen) # True
print(len(seen)) # 4Three operations cover 90% of dedup usage: set(), .add(x), x in s.
seen = set()
for item in items:
if item in seen:
continue # skip duplicate
seen.add(item) # mark as done
process(item) # do the actual workThree-line guard; once you've internalized the pattern you'll write it without thinking.
continue and break| Statement | Effect |
|---|---|
continue | Skip rest of this iteration, go to next |
break | Exit the loop entirely |
Use continue for "skip this one". Use break for "stop now, we found what we wanted / hit a fatal case".
The in-memory seen set vanishes when the script ends. For automations that re-run on a schedule:
seen = set(sheet_read_column()).sheet_append_row([item]) plus seen.add(item).Now re-runs see the same set as last time — true cross-run idempotency. The mechanism (read state, mutate state, write state back) is Auto Intermediate.
A set throws away duplicates and throws away order. If you need both — "unique items in order they first appeared" — you'd use a different structure (a dict keyed on the value, or a list with a parallel set as a guard). Pick the container that matches your need.
Create a free account to get started. Paid plans unlock all tracks.