Sets in Python

Sets

A set is yet another mutable sequence type used to store collection of unique items. You can think of sets just like a list. However, they are different from list in the following ways:

  1. Each element inside a set must be unique.
  2. Elements in a set are stored in no particular order.

If your application doesn’t care about the way elements are stored, use sets instead of lists because when it comes to manipulation of elements, a set is far more efficient than list.

Creating Sets

We can create a set using the following syntax:

Syntax: a_set = { item1, item2, item3 ..., itemN }

Notice that we are using curly braces ({}) here.

We can also use set() constructor function to create sets. Here are some examples:

List comprehension can also be used to create sets.

As already mentioned, sets can only contain unique values. If you try creating a set with duplicate values, Python will automatically remove such values. For example:

Note that although we have used some duplicate elements while creating sets, such values only appears once when sets are printed because a set doesn’t store duplicate elements.

Getting Length of Sets

To determine the number of items in a set use the len() function.

The max(), min(), sum() built-in functions

As with list and tuples, we can also use these functions with sets.

Adding and removing elements

Remember sets are mutable objects and thus we can add or delete elements from sets without creating additional set objects in the process. We use add() and remove() methods to add and remove element from a set respectively.

If you try to remove an element which doesn’t exists in the set, the remove() method will throw a KeyError exception.

In case this behavior is not desired use the discard() method. The discard() method also removes the element from the set but in case the element is not found, it silently ignores the error.

We can also add multiple elements to the set at once using the update() method. The update() method accepts an object of iterable type, such as list, tuple, string etc.

Note that it is the individual elements of the object that becomes elements of the set not the object itself.

To remove all the elements from a set use the clear() method.

Looping through sets

Just as with other sequence types, we can use for loop to iterate over the elements of a set.

Membership Operator in and not in

As usual, we can use use in and not in operators to find the existence of an element inside a set.

Subset and Supersets

Set A is a subset of B, if all the elements in set A is also the elements in set B. For example:

A is a set of first 10 natural numbers and B is a set of first 3 natural numbers. All the elements in set B are also the elements in set B. Hence B is a subset of A. In other words, we can also say that set A is a superset of B.

We can test whether a set is a subset or superset of another set using issubset() and issuperset() methods.

Comparing Sets

We can also use relational operators to test whether a set is a subset or superset of another set. For example:

The == and != operators can be used to test if two sets are same i.e they contain the same elements.

Union and Intersection of Sets

Union of Sets: Say we have two sets A and B, then the union of A and B is a set which consists of all the elements from A and all the elements from B. Duplicate elements will be included only once. In Mathematics we use symbol to denote union. Symbolically, we write A union B as A ∪ B.

For example:

To perform union operation in Python, we can use union() method or | operator. For example:

Intersection of sets: The intersection of sets A and B is a set which consists of all elements common to both A and B. The symbol denotes intersection. Symbolically, A intersection B is written as A ∩ B. For example:

To perform intersection operation in Python, we use intersection() method or & operator. For example:

Difference and Symmetric Difference of Sets

Difference of sets: The difference of sets A and B is a set of elements which contains all the elements from set A but not B. As usual we use - symbol to denote difference operation. Symbolically, A minus B is written as A - B. For example:

In Python, we use difference() method or - operator to perform the set difference.

Symmetric Difference of sets: Symmetric difference of two sets A and B is a set which consists of elements that are in one set but not in both. The symbol denotes symmetric difference. For example:

In Python, we use symmetric_difference() method or ^ operator to perform this operation.

Leave a Comment