A set is an unordered collection of zero or more immutable Python data objects.
>>> {3, 6, "cat", False}Operations on a Set
- Membership —
in— set membership. - Length —
len— returns the cardinality of the set. |—aset | otherset— returns a new set with all elements from both sets.&—aset & otherset— returns a new set with only the elements common to both sets.-—aset - otherset— returns a new set with all items from the first set not in the second.<=—aset <= otherset— asks whether all elements of the first set are in the second.
Set Methods
union—aset.union(otherset)— returns a new set with all elements from both sets.intersection—aset.intersection(otherset)— returns a new set with only the elements common to both sets.difference—aset.difference(otherset)— returns a new set with all items from the first set not in the second.issubset—aset.issubset(otherset)— asks whether all elements of one set are in the other.add—aset.add(item)— addsitemto the set.remove—aset.remove(item)— removesitemfrom the set.pop—aset.pop()— removes an arbitrary element from the set.clear—aset.clear()— removes all elements from the set.