# An Introduction to Python Sets — Part II

## Operations on set objects in Python

Grouping objects into a set can be useful in programming as well, and Python provides a built-in set type to do so. In the previous part, we learned about:** ****How to define set objects in Python****.**

Now, we will discover some of the **standard data structure operations** like size, adding elements, deleting elements and modifying a set. Set operations like union, intersection, etc. will be discussed in the next part.

Methods that will be discussed in this story are as follows:

**Main operations**: len, in (membership)**Data structure**: add, update, discard, remove, pop, clear

### Set Size and Membership

The standard `len()`

function can be used to return the **number of elements** in a set.

>>> my_set = {'a', 'd', 'b', 'c'}

>>> len(my_set)

4

The property of a set having distinct elements creates a unique opportunity. We can use this function to **calculate the number of unique elements** in a list.

We first convert a list into a set and then, use `len()`

function to count the number of elements. Finally, we store the unique elements in `unique_list`

by converting set back to a list.

Note: The order of elements in the set is different from the list. Use **ordered sets** to maintain order.

To check if an element is present in the set or not, we use the `in`

and `not in`

operators:

### Data Structure Operations on Sets

Sets are mutable. But since they are unordered, many data structure operations like **indexing and slicing have no meaning**. Thus we cannot access or change an element of a set.

This means that while we can change what elements reside in a set, we cannot modify an element inside it.

**Inserting elements in a set:**

`add()`

: add a single element to a set`update`

: add multiple elements to a set

In both cases, duplicates are avoided.

`adds`

x.add(<item>)`, a single immutable object, to`

<item>

x

The update method is extremely useful as you can even s**end tuples, strings and lists (apart from sets) as its argument as well**.

`adds to`

x.update(a)`any elements in`

x`that`

a`does not already have`

x

**Removing elements from a set:**

`remove()`

or`discard()`

: remove a specific element from a set`pop()`

: remove and return the last element from a set`clear()`

: empty the set

`and`

x.remove(<item>)`removes`

x.discard(<item>)`from`

<item>`.`

x

The difference is that if you use`remove(<item>)`

, Python raises an exception if`<item>`

is not in`x. But no exception is raised if discard(<item>) is used.`

*x.pop()*

removes the last element of a set.

As a set is unordered, an element is arbitrarily chosen, removed and returned. If `x`

is empty, `x.pop()`

**raises an exception**.

`removes all elements from`

x.clear()

x

>>> my_set = {'a', 'd', 'b', 'c'}

>>> my_set.clear()

>>> my_set

set([])

### Conclusion

As sets are unordered, slicing and indexing operations don’t make sense.

Use add method to insert single elements and update to insert multiple.

Remove and discard are used to delete a specific element from the set. But, remove will give a warning if the element is not present.

### Helpful Links

Part I of this story is available here.

Part III of this story is available here.

If you like this story, you might also like my story on List Comprehension in Python and Functions as Objects in Python.