Set is an
unordered collection of elements with no duplicates. We can perform union,
intersection, difference, and symmetric difference operations on sets.
How to create set
You can
create set using {} (or) set() function. ‘set()’ creates empty set.
Observe
above snippet, evenNumbers is a set that contains even numbers. Observe the
output, set doesn’t contain duplicate elements.
Following
operations are supported by set.
len(s) : cardinality of set
Returns cardinality(Number of distinct elements)
of the set.
>>> evenNumbers={2, 4, 6, 8, 8, 4, 10} >>> len(evenNumbers) 5
x in s : Check whether element is in set or not
‘in’
operator is used to check whether element is in set or not, return true if the
element is set, else false.
>>> evenNumbers {8, 10, 2, 4, 6} >>> >>> 100 in evenNumbers False >>> >>> 2 in evenNumbers True
x not in s : Check whether element is in set or not
‘not in’
operator is opposite of ‘in’ operator, return true if the element is not in
set, else false.
>>> evenNumbers {8, 10, 2, 4, 6} >>> >>> 10 not in evenNumbers False >>> >>> 100 not in evenNumbers True
isdisjoint(other)
Return true if two sets are disjoint, else
false. Two sets are said to be disjoint if they have no element in common.
>>> evenNumbers {8, 10, 2, 4, 6} >>> >>> evenNumbers.isdisjoint({1, 3, 5, 7}) True >>> >>> evenNumbers.isdisjoint({1, 3, 5, 7, 8}) False
issubset(other)
Return true, if this set is subset of other,
else false.
>>> evenNumbers {8, 10, 2, 4, 6} >>> >>> evenNumbers.issubset({2, 4}) False >>> >>> evenNumbers.issubset({2, 4, 6, 8, 10, 12}) True
set <= other
Return true
if every element in the set is in other.
set < other
Return true, if the set is proper subset of
other, that is, set >= other and set != other.
>>> evenNumbers {8, 10, 2, 4, 6} >>> >>> evenNumbers <= {2, 4, 6, 8, 10} True >>> evenNumbers <= {2, 4, 6, 8, 10, 12} True >>> >>> evenNumbers < {2, 4, 6, 8, 10} False >>> evenNumbers < {2, 4, 6, 8, 10, 12} True
Union of two sets
union(other, ...)
‘set
| other | ...’
>>> evenNumbers={2, 4, 6, 8, 10} >>> oddNumbers={1, 3, 5, 7, 9} >>> result=evenNumbers|oddNumbers >>> result {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Intersection of two sets
intersection(other, ...)
‘set
& other & ...’
>>> evenNumbers={2, 4, 6, 8, 10} >>> powersOf2={1, 2, 4, 8, 16} >>> result=evenNumbers&powersOf2 >>> result {8, 2, 4}
Difference between two sets
difference(other, ...)
‘set - other - ...’
Return a new set with elements in the set that
are not in the others.
>>> evenNumbers {8, 10, 2, 4, 6} >>> powersOf2 {16, 8, 2, 4, 1} >>> evenNumbers-powersOf2 {10, 6} >>> powersOf2-evenNumbers {16, 1}
Symmetric difference between two sets
symmetric_difference(other)
set ^ other
If A and B
are two sets, then Simmetric difference between A and B is A^B = (A-B) union
(B-A)
>>> evenNumbers {8, 10, 2, 4, 6} >>> powersOf2 {16, 8, 2, 4, 1} >>> evenNumbers-powersOf2 {10, 6} >>> powersOf2-evenNumbers {16, 1} >>> >>> evenNumbers^powersOf2 {1, 6, 10, 16}
Copy elements of set
‘copy’
function return a new set with a shallow copy of s.
>>> evenNumbers {8, 10, 2, 4, 6} >>> temp=evenNumbers.copy() >>> temp {8, 10, 2, 4, 6}
Update the set
update(other, ...)
set |= other | ...
Update the
set by adding elements from other sets.
>>> evenNumbers {8, 10, 2, 4, 6} >>> oddNumbers {9, 3, 5, 1, 7} >>> powersOf2 {16, 8, 2, 4, 1} >>> >>> evenNumbers.update(oddNumbers, powersOf2) >>> evenNumbers {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16}
Intersection of all sets
intersection_update(other,
...)
set &=
other & ...
>>> numbers {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16} >>> oddNumbers {9, 3, 5, 1, 7} >>> powersOf2 {16, 8, 2, 4, 1} >>> numbers.intersection_update(oddNumbers, powersOf2) >>> numbers {1}
Difference update
difference_update(other, ...)
set -= other | ...
Update the set, removing elements found in
others.
>>> oddNumbers {9, 3, 5, 1, 7} >>> powersOf2 {16, 8, 2, 4, 1} >>> oddNumbers.difference_update(powersOf2) >>> oddNumbers {9, 3, 5, 7}
Symmetric difference update
symmetric_difference_update
set ^= other
Update the
set, keeping only elements found in either set, but not in both.
>>> oddNumbers {9, 3, 5, 7} >>> powersOf2 {16, 8, 2, 4, 1} >>> oddNumbers.symmetric_difference_update(powersOf2) >>> oddNumbers {1, 2, 3, 4, 5, 7, 8, 9, 16}
Add element to the set
‘add’ method
is used to add element to set.
>>> temp {2, 3, 5, 7} >>> >>> temp.add(11) >>> temp.add(13) >>> >>> temp {2, 3, 5, 7, 11, 13}
Remove an element from set
‘remove’
method is used to remove element from set.
>>> temp {2, 3, 5, 7, 11, 13} >>> >>> temp.remove(2) >>> temp {3, 5, 7, 11, 13} >>> >>> temp.remove(11) >>> temp {3, 5, 7, 13}
Throws
KeyError, if element is not in the set.
>>>
temp.remove(100)
Traceback
(most recent call last):
File "<stdin>", line 1, in
<module>
KeyError:
100
Remove arbitrary element from set
‘pop()’ is
used to remove and return an arbitrary element from the set. Throws KeyError,
if the set is empty.
>>> temp {5, 7, 13} >>> temp.pop() 5 >>> >>> temp.pop() 7 >>> temp.pop() 13 >>> temp.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set'
Remove all elements from set
‘clear’
method is used to remove all elements from set.
>>> powersOf2 {16, 8, 2, 4, 1} >>> >>> powersOf2.clear() >>> powersOf2 set()
No comments:
Post a Comment