colors = {"red", "green", "blue"}
colors.add("red") # already there — ignored
colors.add("yellow")
print(colors) # {'red','green','blue','yellow'} (order varies)
raw = ["alice@x.com", "bob@y.com", "alice@x.com", "carol@z.com"]
unique = list(set(raw))
last_month = {"Alice", "Bob", "Carol"}
this_month = {"Bob", "Carol", "Dave"}
last_month | this_month # union {Alice, Bob, Carol, Dave}
last_month & this_month # intersection {Bob, Carol}
last_month - this_month # difference {Alice} — churned
this_month - last_month # difference {Dave} — new
That last pair — "who churned" and "who's new" — is a classic real-world use.
Checking x in big_list scans every item. Checking x in big_set is nearly instant, regardless of size. If you're checking membership a lot, use a set.
list(set(x)) is the fastest dedupe trick.| union, & intersection, - difference.Given last month's customers (a list) and this month's customers (a list), print the count of churned and new customers.