Sets keep each value once. Remove duplicates in one step and compare groups with &, | and -.
Python · beginner · 6 min · 9 steps · free
rolls = [3, 1, 3, 6, 1, 3]
seen = set(rolls) # duplicates disappear
seen.add(4)
seen.add(3) # already there: nothing changes
evens = {2, 4, 6}
both = seen & evens # in both sets
either = seen | evens # in at least one
only_seen = seen - evens # in seen but not in evens
print(seen, both, either, only_seen, 6 in seen)rolls is created and points to a new list: [3, 1, 3, 6, 1, 3].seen points to, index 2 changes from 6 to 4 and index 3 changes from null to 6.evens is created and points to a new set: [2, 4, 6].seen but not in evens: {1, 3}.Answer: 2. Duplicates are dropped.
Answer: {1, 2, 3}. Union combines both.
← Dictionaries: Keys and Values · List and Dict Comprehensions →