Python Set Comprehension


Python Set Comprehension

In Python, Set Comprehension is used to create a new set of items from a given iterator. Set Comprehension offers a shorter syntax to create sets from an iterator, unlike looping statements.

We can also include conditions on which items to include in the set, or apply transformations on elements of the given iterator, while creating sets using Set Comprehension.

In this tutorial, we will go through different scenarios to understand how Set Comprehension can be written or used in Python, with example programs.

Syntax of Set Comprehension

The syntax of the expression for Set Comprehension is given below.

{expression for item in iterable if condition}

where for loop is used to iterate over each item in the iterable, and if the item satisfies the given condition, you can transform the item using expression and add it to the set.

Set Comprehension in its simplest form in Python

In this example, we use Set Comprehension to copy elements of a set to another set.

{item for item in iterable}

When you compare the above set comprehension with that of the syntax, we have given the item itself for the expression, and there is no condition. Since there is no condition, all the items are eligible to be added to the new set. Therefore, all the items of the given iterable shall be added to the new set.

This is the Set Comprehension in its simplest form, and gives us a great introduction to the usage of Set Comprehension in Python.

Python Program

set_1 = {"apple", "banana", "cherry"}
set_2 = {x for x in set_1}
print(set_2)

Output

{'cherry', 'banana', 'apple'}

Apply a condition to Set Comprehension in Python

Now, let us apply a condition whether to add the item from the given iterable to the new set.

We take a set of integers, and shall give the condition that the number must be exactly divisible by 5 to make it to the new list.

Python Program

set_1 = [5, 12, 20, 10, 16, 14, 25]

set_2 = {x for x in set_1 if x % 5 == 0}
print(set_2)

Output

{25, 10, 20, 5}

Set Comprehension with Iterables

We already know that we can create a new set from an iterable object using Set Comprehension.

In the following example, we create a set from different iterable objects like: list, range, set, dictionary, string, etc.

1. Set Comprehension with List as input

In the following program, we take a list of strings, and create a set using Set Comprehension with the given list as iterable.

Python Program

list_1 = ['apple', 'banana', 'cherry']
set_1 = {x for x in list_1}
print(set_1)

Output

{'apple', 'banana', 'cherry'}

2. Set Comprehension with Range as input

In the following program, we take a range object, and create a set using Set Comprehension with the given range as iterable.

Python Program

range_1 = range(5)
set_1 = {x for x in range_1}
print(set_1)

Output

{0, 1, 2, 3, 4}

3. Set Comprehension with another Set as input

In the following program, we take a set of strings, and create a set using Set Comprehension with the given set as iterable.

Python Program

set_0 = {'apple', 'banana', 'cherry'}
set_1 = {x for x in set_0}
print(set_1)

Output

{'banana', 'cherry', 'apple'}

4. Set Comprehension with Dictionary keys as input

In the following program, we take a dictionary object, and create a set using Set Comprehension with the keys of dictionary as iterable.

Python Program

dict_1 = {'apple': 28, 'banana': 55, 'cherry': 64}
set_1 = {x for x in dict_1}
print(set_1)

Output

{'banana', 'cherry', 'apple'}

5. Set Comprehension with Dictionary values as input

In the following program, we take a dictionary object, and create a set using Set Comprehension with the values of dictionary as iterable.

Python Program

dict_1 = {'apple': 28, 'banana': 55, 'cherry': 64}
set_1 = {x for x in dict_1.values()}
print(set_1)

Output

{64, 28, 55}

6. Set Comprehension with String as input

In the following program, we take a string object, and create a set using Set Comprehension with the string as iterable.

Python Program

str_1 = "apple"
set_1 = {x for x in str_1}
print(set_1)

Output

{'p', 'e', 'a', 'l'}

Transform items in Set Comprehension

We have seen in the syntax section that we can apply transformations on the items in the Set Comprehension.

In the following example, we take a list of strings as input, and create a new set with all the elements from the input list transformed to uppercase.

Python Program

list_1 = ['apple', 'banana', 'cherry']
set_1 = {x.upper() for x in list_1}
print(set_1)

Output

{'CHERRY', 'APPLE', 'BANANA'}

Summary

In this tutorial of Python Sets, we got introduced to Set Comprehension in Python. We have seen the syntax, and gone through different scenarios of creating sets using Set Comprehension, with examples.