Next / Previous / Contents / TCC Help System / NM Tech homepage

15.1. Operations on mutable and immutable sets

These operations are supported by both set and frozenset types:

x in S

Predicate that tests whether element x is a member of set S.

>>> 1 in set([0,1,4])
True
>>> 99 in set([0,1,4])
False

x not in S

Predicate that tests whether element x is not a member of set S.

>>> 1 not in set([0,1,4])
False
>>> 99 not in set([0,1,4])
True

S1 == S2

Predicate that tests whether sets S1 and S2 have exactly the same members.

>>> set('bedac') == set('abcde')
True
>>> set('bedac') == set('bedack')
False

S1 != S2

Predicate that tests whether sets S1 and S2 have different members.

>>> set ( 'bedac' ) != set ( 'abcde' )
False
>>> set('bedac')!=set('bedack')
True

S1 < S2

Predicate that tests whether S1 is a proper subset of S2; that is, all the elements of S1 are also members of S2, but there is at least one element of S2 that is not in S1.

>>> set('ab') < set('ab')
False
>>> set('ab') < set('abcde')
True

S1 > S2

Predicate that tests whether S1 is a proper superset of S2; that is, all the elements of S2 are also members of S1, but there is at least one element of S1 that is not in S2.

>>> set('ab') > set('ab')
False
>>> set('abcde') > set('cd')
True

S.copy()

Return a new set of the same type as S, containing all the same elements.

>>> s1=set('aeiou')
>>> s2=s1
>>> s3=s1.copy()
>>> s1.add('y')
>>> s1
set(['a', 'e', 'i', 'o', 'u', 'y'])
>>> s2
set(['a', 'e', 'i', 'o', 'u', 'y'])
>>> s3
set(['a', 'i', 'e', 'u', 'o'])

S1.difference(S2)

Returns a new set of the same type as S1, containing only those values found in S1 but not found in S2. The S2 argument may be a set or a sequence.

>>> set('roygbiv').difference('rgb')
set(['i', 'o', 'v', 'y'])

S1 - S2

Same as S1.difference(S2), except that S2 must be a set.

>>> set('roygbiv') - set('rgb')
set(['i', 'y', 'o', 'v'])

S1.intersection(S2)

Returns a new set, of the same type as S1, containing only the elements found both in S1 and S2.

S2 may be a set or a sequence.

>>> set([1,2,3,5,7,11]).intersection(set([1,3,5,7,9]))
set([1, 3, 5, 7])
>>> set([1,3,5]).intersection( (2,4,6,8) )
set([])

S1 & S2

Same as S1.intersection(S2), but S2 must be a set.

S1.issubset(S2)

Predicate that tests whether every element of S1 is also in S2. S2 may be a set or a sequence.

>>> set([1,2]).issubset(set([2,4,1,8]))
True
>>> set([2,4,1,8]).issubset(set([1,2]))
False
>>> set(['r', 'g', 'b']) <= set(['r', 'o', 'y', 'g', 'b', 'i', 'v'])
True

S1 <= S2

Same as S1.issubset(S2), but S2 must be a set.

S1.issuperset(S2)

Predicate that tests whether every element of S2 is also in S1. S2 may be a set or a sequence.

>>> set([1,2]).issuperset(set([2,4,1,8]))
False
>>> set([2,4,1,8]).issuperset(set([1,2]))
True

S1 >= S2

Same as S1.issuperset(S2).

S1.symmetric_difference(S2)

Returns a new set of the same type as S1, containing only elements found in S1 or S2, but not found in both. The S2 argument may be a set or a sequence.

>>> set('aeiou').symmetric_difference('etaoin')
set(['n', 'u', 't'])

S1 ^ S2

Same as S1.symmetric_difference(S2), but S2 must be a set.

S1.union(S2)

Returns a new set, with the same type as S1, containing all the elements found in either S1 or S2.

The S2 argument may be a set or a sequence.

>>> set([1,2]).union(set([1,3,7]))
set([1, 2, 3, 7])
>>> set([1,2]).union( (8,2,4,5) )
set([8, 1, 2, 4, 5])

S1 | S2

Same as S1.union(S2).