Home Lesson-7.1

Lesson-6.5

Sets

Introduction

A set is an unordered collection with no duplicate elements [refer]. Unlike lists and tuples, there is no notion of order in a set. This is why it is called an unordered collection as opposed to a sequence. A set can be defined as follows:

Notice the similarity in syntax between sets and dictionaries. Both are enclosed within curly braces. While a dictionary has key-value pairs in it, a set just has a collection of values. A set in Python is a remarkably accurate representation of a mathematical set. Therefore, most of the properties that you are used to seeing in mathematical sets nicely carry over to Python sets. This connection is so strong that you can often forget that you are dealing with Python sets.

As stated before, sets do not support duplicate elements. We see that nums_1 and nums_2 are equal sets. However, they don't point to the same object. Sets support membership just like lists, tuples and dictionaries.

The number of elements in a set, which is the same as its cardinality, is given by the len function:

Sets cannot be indexed. This is quite reasonable as they are not ordered collections. The following code will throw an error:

Any hashable object can be added to sets. This means most of the immutable types such as int, float, str and tuple can be added to sets. A small caveat as far as tuples are concerned: a tuple of lists is unhashable and therefore cannot be added to sets.

not_a_set returns a TypeError as expected.

 

Iterating through Sets

Though a set is not a sequence, iterating through the elements of a set is supported.

 

Growing Sets

How do we define an empty set?

We see that empty_set is in fact an empty dictionary. Computers are precise machines, which makes them very faithful. Few lessons back we used { } to initialize an empty dictionary. It hasn't changed. { } is still an empty dictionary. So, how do we define an empty set then?

Simple enough! With the empty set and set-iteration defined, we can now grow sets from scratch.

Consider the first 100 powers of 7:

Note down the last digit of each of these powers. How many of them are unique? What are these numbers?

This problem has a simple mathematical solution. But humor me and assume that you don't know how to solve this problem. Let us go for a computational solution.

add is a method used to add elements to a set. The solution to this problem is a typical use case of sets. When you expect duplicate elements to come up often and if you are not concerned with duplicates, then sets are ideal objects for storage. The same problem can be solved using lists:

 

Set Operations

Mathematical sets are friendly objects. They routinely interact with each other through one of the following operations:

Python sets strive to be as friendly as their mathematical counterparts. We will see how each of these operations are represented:

Both lines return the value True. A set is a proper subset of if every element in is present in and . It is denoted by . That is, there is at least one element in which is not in :

The A < B operator checks if A is a proper subset of B. In this case A is not a proper subset of B, so the second print statement returns False.

When there are multiple sets, we could do the following:

What happens if there are no elements in common? We should get the empty set:

We have used an assert statement just to introduce some variation. As it doesn't raise an AssertionError, we are right on target.

 

Other Set Methods

The methods that we saw in the previous section had a mathematical flavor. Now, we shall look at those methods that have a computational flavor!

To remove an element from the set, we can use the remove method:

If we try to remove an element that is not present in the set, the interpreter will throw a KeyError:

Consider the following problem:

Given a list L, extract all unique elements from it and store the result in another list, L_uniq. The order of elements does not matter.

Let us first look at a solution that doesn't use sets:

Now, for some set magic:

Passing a list to the set function removes all duplicates and returns the unique elements.

 

Mutability

Sets are mutable entities.

A and B are the same objects. As before, there are two ways to do a shallow copy: