15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

7.9. Set Type Built-in Methods<br />

7.9.1. Methods (All Set Types)<br />

We have seen the operator equivalents to most of the built-in methods, summarized in Table 7.4.<br />

Method Name Operation<br />

Table 7.4. Set Type Methods<br />

s.issubset(t) Returns TRue if every member of s is in t, False otherwise<br />

s.issuperset(t) Returns true if every member of s is in t, False otherwise<br />

s.union(t) Returns a new set with the members of s or t<br />

s.intersection(t) Returns a new set with members of s and t<br />

s.difference(t) Returns a new set with members of s but not t<br />

s.symmetric_difference(t) Returns a new set with members of s or t but not both<br />

s.copy() Returns a new set that is a (shallow) copy of s<br />

The one method without an operator equivalent is copy(). Like the dictionary method of the same name,<br />

it is faster to create a copy of the object using copy() than it is using a factory function like set(),<br />

frozenset(), or dict().<br />

7.9.2. Methods (Mutable Sets Only)<br />

Table 7.5 summarizes all of the built-in methods that only apply to mutable sets, and similar to the<br />

methods above, we have already seen most of their operator equivalents.<br />

Method Name Operation<br />

Table 7.5. Mutable Set Type Methods<br />

s.update(t) Updates s with elements added from t; in other words, s now has<br />

members of either s or t<br />

s.intersection_update(t) Updates s with members of both s and t<br />

s.difference_update(t) Updates s with members of s without elements of t

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!