Adding elements to sets

To add a single element to a set use the add() method:

>>> k = {81, 108}
>>> k
{81, 108}
>>> k.add(54)
>>> k
{81, 108, 54}
>>> k.add(12)
>>> k
{81, 108, 54, 12}

Adding an element that already exists has no effect:

>>> k.add(108)

Although neither does it produce an error. Multiple elements can be added in one go from any iterable series, including another set, using the update() method:

>>> k.update([37, 128, 97])
>>> k
{128, 81, 37, 54, 97, 12, 108}

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset