Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Set Comprehension


Set Comprehension

The set comprehension offers shorter syntax to create a new set from the existing set or any iterable (List, Tuple, or Dictionary).

A comprehensions consist of a single expression followed by at least one for statement and zero or more for or if statements.

While using set comprehension, your for loop code should be enclosed in curly braces.

It will return a new set while keeping the old set unchanged when using set comprehension.

The Syntax
new_set = {expression for item in iterable if condition == True}

expression: This is the operation or value you want to include in the new set for each item in the iterable.

item: This represents each individual item in the iterable.

iterable: This is the original collection of items you're iterating over (e.g., a list, tuple, set, dictionary, or string).

condition (optional): You can include a condition to filter items based on certain criteria. Only items that satisfy the condition will be included in the new set.


Where Use Set Comprehension?

Identify color names containing the letter "n" in the set.

Without set comprehension, you need to write a for loop containing a condition statement.

Example
colors_set = {"Red", "Blue", "Green", "Orange", "White"}
colors_filter_set = set()

for color in colors_set:
    if "n" in color:
        colors_filter_set.add(color)

print(colors_filter_set)
{'Green', 'Orange'}

In the above example, we are identifying color names that contain the letter "n" and adding them to the new set.

But with set comprehension, we can do these things in one single line of code.

Example
colors_set = {"Red", "Blue", "Green", "Orange", "White"}

colors_filter_set = {color for color in colors_set if "n" in color}

print(colors_filter_set)
{'Green', 'Orange'}

Here we are identifying color names that contain the letter "n" with the set comprehension.

As you can see, both outputs are the same with and without set comprehension.


Set Comprehension With Condition

As a filter in the set comprehension, the items accept that only if the statement condition is True.

Example
colors_set = {"Red", "Blue", "Green"}

colors_filter_set = {i for i in colors_set if "Blue" in i}

print(colors_filter_set)
{'Blue'}

In the above example, the condition if i in "Blue" will return True for the "Blue" color item, resulting in a new set containing only the "Blue" color.


Set Comprehension Without Condition

It is optional to include the condition in a set comprehension.

Example
colors_set = {"Red", "Blue", "Green"}

colors_filter_set = {i for i in colors_set}

print(colors_filter_set)
{'Red', 'Blue', 'Green'}

In the above example, we created a new color set without an if condition using the set comprehension. As a result, the new set contains all items from the original set.


Expression

The expression represents the current item in the iteration it can manipulate before adding a new set.

Example
colors_set = {"Red", "Blue", "Green"}

colors_filter_set = {i.upper() for i in colors_set}

print(colors_filter_set)
{'RED', 'BLUE', 'GREEN'}

In the above example, before adding color to the new set, we convert all colors to upper case.