Python Set Comprehension

Python Set Comprehension


A Python set is an unordered collection of unique items that is iterable, mutable, and has no duplicate items. You can perform mathematical set operations such as union, intersection, set difference, etc on Python sets.

The set comprehension is similar to the Python list or dictionary comprehension it provides a shorter syntax to create sets in Python. In this article, you will learn Python list comprehension and its usage.

Syntax of Python set comprehension

You can create a set in python using set comprehension in the given way –

set_var= {x for x in iterable}

Where set_var is a set variable, item x will get added to set_var every time for loop iterate over iterable. Some examples of creating sets are given ahead in this article.

Example of Python set using set comprehension

In our example, we will create a set that will contain integers from 1 to 10. First, we will see the usual way in which a Python set is created and then we will create the same set using set comprehension.

for x in range(1,11):

Using set comprehension

Using set comprehension you can reduce the number of lines of code. You can write the same code as given below –

num={x for x in range(1,11)}

you can see the output in the given image –

Using conditional statements in set comprehension

You can use conditional statements in set comprehension to modify the output on the basis of a condition. We will understand this with the help of an example.

Now suppose we want a set of even numbers between 1 and 11. For this, we need to apply a condition to find the even numbers and add them to the set of even numbers.

The given code will create a set of even numbers between 1 and 11 using set comprehension.

even_num={x for x in range(1,11) if x%2==0}

You can see the output in the given image –

Nested set comprehension

Using a set inside another is called a nested set. You can’t use a set inside another set that means the nesting of the set is not possible instead you can do this by using the inner set as frozenset.

Now see the given example –

var_num={frozenset([i,j]) for j in range(4,7) for i in range(6,8)}

You can see the output in the given image –


This is how you can use set comprehension to write shorter codes. Now If you have any query then write us in the comments below.

Previous Python Dictionary Comprehension
Next Python Iterator vs Iterable

About author

You might also like

Python Programming 0 Comments

Python enumerate() function

Sometimes you may want to track the position of items in an iterable. You can do this in various ways, for example, you can create a counter variable and initialize

Programming 0 Comments

Using Python pass keyword

The Python pass keyword which is a statement in itself basically does nothing. It can be used as a placeholder for future code. As empty code is not allowed in

Python Programming 0 Comments

Taking Input From Users In Python

Computer programs often need to interact with users to get some kind of data from them. Later this data is processed and output is displayed on the console. For example,


No Comments Yet!

You can be first to comment this post!

Leave a Reply