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:

  1. Main operations: len, in (membership)
  2. 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.

Count unique elements in a list using set

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:

membership operations on a set

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:

  1. add(): add a single element to a set
  2. update: add multiple elements to a set

In both cases, duplicates are avoided.

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

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

x.update(a) adds to x any elements in a that x does not already have
update( ) method

Removing elements from a set:

  1. remove() or discard(): remove a specific element from a set
  2. pop(): remove and return the last element from a set
  3. clear() : empty the set
x.remove(<item>) and x.discard(<item>) removes <item> from x.
discard( ) and remove( )
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.

pop( ) method
x.clear() removes all elements from 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

  1. What is a set?
  2. Python Sets

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.