Sets
Set is unordered mutable collection of unique elements
Set is just like dictionary without values. Sets do not support indexing, slicing, or other sequence-like behavior.
Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
Elements can be only hashable objects - just like keys of dictionary (all immutable objects and instances of classes).
Ways to create a set:
set()
{1, 2, 3}
set([1, 2, 3, 4])
set("abcdeabcde")
🪄 Code:
📟 Output:
Main methods
🪄 Code:
📟 Output:
A lot of different methods for checking various mathematical set properties
Set methods
Method(s) | Description |
---|---|
| Return int - length of the set |
| Return True/False - is object |
| Return new set - a union (all from both) |
| Return new set - a intersection (common to both) |
| Return new set - a difference (those in |
| Return True/False - a issubset ( |
| Return True/False - a issubset ( |
Last updated