Frozenset
A set that can't be changed after it's made, so it can go inside another set or be a dictionary key.
Two more things you'll reach for. When you need to dedupe and keep the original order, a set can't do it alone, but a dictionary keeps insertion order:
list(dict.fromkeys(items))keeps the first of each. And when you need a set inside a set, or a set as a dictionary key, use a frozenset, a set that can't change after it's made, which makes it hashable.
Try it now
The right collection usually follows from one question: do you need order, names, or uniqueness?
Learn it properly
Sets: unique items and fast lookups · Duplicates, fast membership, set operations and comparing two lists. About 30 minutes.
Other terms from the same lesson
- hashable: A value that can't change, such as a number, string or tuple, so it can go in a set or be a dictionary key.
- intersection: What two sets have in common: a & b in Python.
- set: A Python collection with no duplicates and no order, built for fast "is this in here?" checks.
- symmetric difference: What's in exactly one of two sets, not both: a ^ b in Python.
- union: Everything that's in either of two sets: a | b in Python.
Never trained an AI?
Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account.