set: Set type
The Set class implements a set of objects. Any objects that can be used as Map keys can be added to a set. The Set class enforces the set constraint: any object may be included in a set at most a single time. An object can be added to a set even when the set already contains that object, but the operation will have no effect. The order of item additions and removals is not significant — a set object only records which objects are currently included in the set, but the objects have no specific order.
- class Set()
- Construct an empty set.
- class Set(iterable)
- Construct and initialize a set. Iterate over the contents of the
argument, which must be an iterable object, and add them to the set.
Example:
Set([1, 3, "x"]) -- Set with items 1, 3 and "x"
Set methods
- length()
- Return the number of objects in the set.
- add(object)
- Add an object to the set. If the object is already in the set, this operation does nothing.
- remove(object)
- Remove an object from the set. Do nothing if the object is not included in the set.
- iterator()
- Return an iterator object that iterates over all the objects in the set in an arbitrary order.
- copy()
- Return a copy of the set with the same items as the original set.
Note: Only item references are copied to the new set — the items itself are not duplicated.
Set operations
Set objects support the following operations:
- x in set
- Test whether a set contains a specific object.
- for x in set
- Set contents can be iterated with a for loop. The objects are iterated in an arbitrary order.
- set1 == set2
- Compare sets for equality. Two sets are equal if they contain the same objects.
- Str(set)
- Return a string representation of a set.
- Hash(set)
- Return the hash value of a set.