Previous Page
Next Page

4.7. Set Operations

Python provides a variety of operations applicable to sets. Since sets are containers, the built-in len function can take a set as its single argument and return the number of items in the set object. A set is iterable, so you can pass it to any function or method that takes an iterable argument. In this case, the items of the set are iterated upon, in some arbitrary order. For example, for any set S, min(S) returns the smallest item in S.

4.7.1. Set Membership

The k in S operator checks whether object k is one of the items of set S. It returns true if it is and False if it isn't. Similarly, k not in S is just like not (k in S).

4.7.2. Set Methods

Set objects provide several methods, as shown in Table 4-4. Nonmutating methods return a result without altering the object to which they apply and can also be called on instances of type frozenset, while mutating methods may alter the object to which they apply and can be called only on instances of type set. In Table 4-4, S and S1 indicate any set object, and x any hashable object.

Table 4-4. Set object methods

Method

Description

Nonmutating methods

 

S.copy( )

Returns a shallow copy of the set (a copy whose items are the same objects as S's, not copies thereof)

S.difference(S1)

Returns the set of all items of S that aren't in S1

S.intersection(S1)

Returns the set of all items of S that are also in S1

S.issubset(S1)

Returns true if all items of S are also in S1; otherwise, returns False

S.issuperset(S1)

Returns true if all items of S1 are also in S; otherwise, returns False (like S1.issubset(S))

S.symmetric_difference(S1)

Returns the set of all items that are in either S or S1, but not in both sets

S.union(S1)

Returns the set of all items that are in S, S1, or in both sets

Mutating methods

 

S.add(x)

Adds x as an item to S; no effect if x was already an item in S

S.clear( )

Removes all items from S, leaving S empty

S.discard(x)

Removes x as an item of S; no effect if x was not an item of S

S.pop( )

Removes and returns an arbitrary item of S

S.remove(x)

Removes x as an item of S; raises a KeyError exception if x is not an item of S


All mutating methods of set objects, except pop, return None.

The pop method can be used for destructive iteration on a set, consuming little extra memory. The memory savings make pop usable for a loop on a huge set, when what you want is to "consume" the set in the course of the loop.

Sets also have mutating methods named difference_update, intersection_update, symmetric_difference_update, and update (corresponding to nonmutating method union). Each such mutating method performs the same operation as the corresponding nonmutating method, but it performs the operation in place, altering the set on which you call it, and returns None. These four nonmutating methods are also accessible with operator syntax: respectively, S-S1, S&S1, S^S1, and S|S1; the corresponding mutating methods are also accessible with augmented assignment syntax: respectively, S-=S1, S&=S1, S^=S1, and S|=S1. When you use operator or augmented assignment syntax, both operands must be sets or frozensets; however, when you call the named methods, argument S1 can be any iterable with hashable items, and the semantics are just the same as if the argument you passed was set(S1).


Previous Page
Next Page