Using the set collection
All of the collections we've looked at previously have been sequences: str
, bytes
, tuple
, and list
have items which can be accessed by their position within the collection. A set
collection is an unordered collection where items are present or absent.
Items in a set
collection must be immutable; they must provide a proper hash value as well as an equality test. This means that we can create sets of numbers, strings, and tuples. We can't easily create a set of lists or a set of sets.
The syntax of a set
display is a sequence of expressions wrapped in {}
.
Here's an example set
built using numbers:
>>> fib_set = {1, 1, 3, 5, 8} >>> fib_set {8, 1, 3, 5}
We've created a set
object by enclosing the values in {}
. This syntax looks ...
Get Python Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.