Python - Set Set methods

Introduction

The set object provides methods to operate on set values.

  • the set add method inserts one item,
  • the set update method is an in-place union, and
  • remove method deletes an item by value.

Demo

x = set('abcde') 
y = set('bdxyz') 

z = x.intersection(y) # Same as x & y 
print( set(['b', 'd']) ) 
z.add('TEST')                  # Insert one item 
print( z ) 

z.update(set(['X', 'Y']))      # Merge: in-place union 
print( z ) 
z.remove('b')                  # Delete one item 
print( z )

Result

Related Topics