Collection of items, in set items are stored in curly { } brackets.
s = {1, 2, 3}
Note: set are unordered, unindexed, and mutable, also it does not allow duplicate values like tuple, list
Access set
s = {1, 2, 3}
print(s)
Access set items
for item in s:
print(item)
Set methods
add() | Add single item to set |
s={1, 2, 3} |
update() | Add multiple items to set |
s={1, 2, 3} |
remove() | Remove item from set, raise an error when item not found |
s={1, 2, 3} |
discard() | Remove item from set, and does not raise an error when item not found |
s={1, 2, 3} |
clear() | Remove all items from a set |
s={1, 2, 3} |
pop() | Remove an element from a set |
s={1, 2, 3} |
difference() | Return set of difference of two set(set of items of A which are not in B) |
x={1, 2, 3} |
intersection() | Return intersection set of two set (set of common items from set A, B) |
x={1, 2, 3} |
union() | Return set of all items from set A, B |
x={1, 2, 3} |
issubset() | Return true if one set is subset of other set otherwise false |
x={1, 2, 3} |
copy() | Return copy of a set |
x={1, 2, 3} |