C. Set Methods

This appendix lists the methods of the set type. It does not cover functions that also may be used with this type, such as len. For those, see Appendix B.

Table C.1. Most Commonly Used Set Methods.

To do the following task:

Call these method(s):

Add element to a set

add

Clear all contents of a set

clear

Copy all contents of another set

copy

Delete an item from a set

discard, pop, remove

Determine if set is a subset or superset of another

issubset, issuperset

Perform set difference (set subtraction)

difference,symmetric_difference

Perform intersection

intersection

Perform union

union

Note: To determine if an element is a member of a particular set, use the in keyword.

set_obj.add(obj)

Adds an object,

Get Supercharged Python: Take Your Code to the Next Level, First Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.