The operations described in this section apply to set (mutable) values, but may not be used with frozenset (immutable) values.
S.add(x)
Add element to set x.
Duplicate elements will be ignored.
S
>>> pbr=set(['USA', 'Brazil', 'Canada'])
>>> pbr.add('Australia')
>>> pbr
set(['Brazil', 'Canada', 'Australia', 'USA'])
>>> pbr.add('USA')
>>> pbr
set(['Brazil', 'Canada', 'Australia', 'USA'])
S.clear()
Remove all the elements from set .
S
>>> pbr set(['Brazil', 'USA']) >>> pbr.clear() >>> pbr set([])
S.discard(x)
If set
contains element S, remove that element from x.
S
If is not
in x, it is
not considered an error; compare S.
S.remove(x)
>>> pbr
set(['Brazil', 'Australia', 'USA'])
>>> pbr.discard('Swaziland')
>>> pbr
set(['Brazil', 'Australia', 'USA'])
>>> pbr.discard('Australia')
>>> pbr
set(['Brazil', 'USA'])
S1.difference_update(S2)
Modify set by removing any values found in S1S2. Value may be a set or a sequence.
S2
>>> s1=set('roygbiv')
>>> s1.difference_update('rgb')
>>> s1
set(['i', 'o', 'v', 'y'])
S1 -= S2
Same as , but S1.difference_update(S2) must be a set.
S2
S1.intersection_update(S2)
Modify set so that it contains only values found in both S1 and S1.
S2
>>> s1=set('roygbiv')
>>> s1
set(['b', 'g', 'i', 'o', 'r', 'v', 'y'])
>>> s1.intersection_update('roy')
>>> s1
set(['y', 'r', 'o'])
S1 &= S2
Same as , but S1.intersection_update(S2) must be a set.
S2
S.remove(x)
If element
is in set x,
remove that element from S.
S
If is not
an element of x, this operation will raise a SKeyError exception.
>>> pbr
set(['Brazil', 'Canada', 'Australia', 'USA'])
>>> pbr.remove('Canada')
>>> pbr
set(['Brazil', 'Australia', 'USA'])
>>> pbr.remove('Swaziland')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Swaziland'
S1.symmetric_difference_update(S2)
Remove from any elements found in both S1 and S1. Value S2 may be a set or a sequence.
S2
>>> s1=set('abcd')
>>> s1.symmetric_difference_update('cdefg')
>>> s1
set(['a', 'b', 'e', 'g', 'f'])
S1 ^= S2
Same as , but S1.symmetric_difference_update(S2) must be a set.
S2
S1.update(S2)
Add to any elements of S1 not found in S2. The
S1
argument may be a set or a sequence.
S2
>>> s1=set('rgb')
>>> s1
set(['r', 'b', 'g'])
>>> s1.update('roygbiv')
>>> s1
set(['b', 'g', 'i', 'o', 'r', 'v', 'y'])
S1 |= S2
Same as , but
S1.update(S2) must be a
set.
S2