Set Comprehension in Python – PythonForBeginners.com

In python programming, we use different data structures like lists, tuples, sets, and dictionaries. Often we create new lists, sets or dictionaries from existing objects in our programs.  In this article, we will study set comprehension and see how it is used in python to create new sets from existing objects in Python. We will also look at some examples of set comprehension in Python.

What is set comprehension in Python?

Set comprehension is a method for creating sets in python using the elements from other iterables like lists, sets, or tuples. Just like we use list comprehension to create lists, we can use set comprehension instead of for loop to create a new set and add elements to it.

Syntax for set comprehension in Python

The syntax for set comprehension is as follows.

newSet= { expression for element in  iterable }

Description of the syntax:

• The iterable may be any iterable object or data structure in Python from which we have to use the elements to create the new set.
• The element denotes the element from the iterable that has to be included in the set.
• The expression can be any mathematical expression derived from the element
• newSet is the name of the new set which has to be created from the elements of the iterable.

Let us discuss this syntax using an example. In the following example, we are given a list of 10 integers. We have to create a set of the triples of these integers. This can be done using set comprehension as follows.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newSet = {element*3 for element in myList}
print("The existing list is:")
print(myList)
print("The Newly Created set is:")
print(newSet)

Output:

The existing list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The Newly Created set is:
{3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

In the above example, we are given a list of 10 integers and we have created a set of triples of the elements of the given list. In the statement “newSet = {element *3 for element in myList}”, Set comprehension is used to create the newSet which contains triples of the elements in myList

If you will compare the code with the syntax for set comprehension, the following observation can be made.

1. mySet is the set whose elements have been used to create a new set. Hence mySet has been used in place of the iterable.
2. We have created a new set with triples of the elements of myList. Hence, element*3 has been used in place of expression.

We can also use conditional statements in a set comprehension. The syntax for using conditional statements in set comprehension is as follows.

newSet= { expression for element in  iterable if condition }

Description of the syntax:

• The iterable may be any iterable object or data structure in Python from which we have to use the elements to create the new set.
• The condition is a conditional expression using
• The element denotes the element from the iterable that has to be included in the set.
• The expression can be any mathematical expression derived from the element
• newSet is the name of the new set which has to be created from the elements of the iterable.

Let us discuss this syntax using an example. In the following example, we are given a list of 10 integers. We have to create a set of triples of even integers. This can be done using set comprehension as follows.

myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newSet = {element*3 for element in myList if element % 2 ==0}
print("The existing list is:")
print(myList)
print("The Newly Created set is:")
print(newSet)

Output:

The existing list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The Newly Created set is:
{6, 12, 18, 24, 30}

In the above example, we have a list of 10 integers and we have created a set of triples of those elements of the given list which are even numbers. In the statement “newSet = {element *3 for element in myList if element % 2 == 0}”, Set comprehension is used to create the newSet which contains squares of the even elements in myList

If we compare the code with the syntax for set comprehension, the following observation can be made.

1. myList is the list whose elements have been used to create a new set. Hence myList has been used in place of the iterable.
2. We have to create a new list with triples of the even elements of myList. Hence, element*3 has been used in place of expression.
3. To select only the even elements, we have used a conditional statement “element % 2 == 0” at the place of condition.

Examples of set comprehension

Now that we have understood the syntax for set comprehension in python, we will some examples to understand the concept in a better way.

Create a set from elements of another set

If you have to create a set using elements of another set, you can do so by creating a new set. After creating a new set, you can add elements to the new set using add() method and for loop . In the following example, we have created a new set with squares of element of an existing set.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newSet = set()
for element in mySet:
print("The existing set is:")
print(mySet)
print("The Newly Created set is:")
print(newSet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The Newly Created set is:
{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

In the above example, initializing an empty set and then using add() method to add elements to it is inefficient. Instead of this, we can directly initialize the new set with all the elements using the set comprehension as follows.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newSet = {element ** 2 for element in mySet}
print("The existing set is:")
print(mySet)
print("The Newly Created set is:")
print(newSet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The Newly Created set is:
{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}

Filter elements from a set based on a condition

We can create a new set from elements of an old set by applying some conditions to the elements. To do this using a for loop, you can use a conditional statement and the add() method as follows. In the following example, we have filtered even numbers from a set.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newSet = set()
for element in mySet:
if element % 2 == 0:
print("The existing set is:")
print(mySet)
print("The Newly Created set is:")
print(newSet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The Newly Created set is:
{2, 4, 6, 8, 10}

Instead of the for loop, you can filter out elements from old set to create a new set using set comprehension as follows.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newSet = {element for element in mySet if element % 2 == 0}
print("The existing set is:")
print(mySet)
print("The Newly Created set is:")
print(newSet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The Newly Created set is:
{2, 4, 6, 8, 10}

Delete elements from a set

If you have to delete some elements from a set, you can create a new set from the elements that have not to be deleted. After that, you can assign the new set to the old set variable as follows. In the following example, we have deleted all the odd elements from a set.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
print("The existing set is:")
print(mySet)
mySet = {element for element in mySet if element % 2 == 0}
print("The modified set is:")
print(mySet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The modified set is:
{2, 4, 6, 8, 10}

Change the data type of elements of the set

We can also change the data types of the elements of a set using set comprehension as shown in the following example. Here, we have converted all the integer elements of a set to strings.

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
newSet = {str(element) for element in mySet }
print("The existing set is:")
print(mySet)
print("The Newly Created set is:")
print(newSet)

Output:

The existing set is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The Newly Created set is:
{'2', '4', '8', '6', '3', '7', '1', '10', '5', '9'}

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Latest articles

Related articles