## Introduction

The Python set comprehension is a concise and efficient way to create sets. It allows you to generate new sets by applying an expression to each item in an existing iterable (such as a list, tuple, set, or string) and filtering the items based on specified conditions.

Set comprehensions are very similar in nature to list comprehensions except that items are placed in a Python set instead of a list. And since sets are unordered collections of unique items, any duplicates items are eliminated and there is no guarantee of any particular order of the items in the resulting set.

## Basic Python Set Comprehension Syntax

`The basic set comprehension syntax has curly braces {} that surround an expression followed by `

for loop-like syntax.

{expression for item in iterable}

`expression`

: An expression that produces a value for each item.`item`

: A variable representing each item in the iterable.`iterable`

: The original iterable being processed.

Aside from using curly braces `{}`

, the set comprehension syntax is identical to the list comprehension syntax, (which uses square brackets `[]`

).

## Creating a Simple Set With a Set Comprehension

Let’s continue learning about set comprehensions with a simple example. Suppose you want to create a set of the squares of integers from -5 to 5. Since negative numbers have positive squares, the squares of the sequence of integers from -5 to 5 will have duplicates, and the set comprehension will eliminate those.

**Example:**

squares_set = {x**2 for x in range(-5, 6)} print(squares_set)

**Output:**

{0, 1, 4, 9, 16, 25}

In this example:

`x**2`

: The expression that calculates the square of each number.`range(-5, 6)`

: An iterable that generates the integers -5 to 5.`for x in range(-5, 6)`

: Iterates through values -5 to 5.

The set comprehension iterates through the integers -5 to 5, adding all their squares to a set. The set eliminates duplicate values, resulting in `{0, 1, 4, 9, 16, 25}`

.

## Differences Between Set Comprehensions and List Comprehensions

If you have read the list comprehensions lesson, you now also know all about set comprehensions and the rest of the lesson will be very familiar.

Set comprehensions are different from list comprehensions in only these two ways:

- Set comprehensions use curly braces instead of square brackets.
- Set comprehensions generate sets instead of lists.

## Using *if* Clauses in Set Comprehensions

Like list comprehensions, set comprehensions can also include an optional `if`

clause to filter elements from the original iterable based on a condition.

The syntax for using an `if`

clause in a set comprehension is similar to that in list comprehensions:

{expression for item in iterable if condition}

`condition`

: A boolean expression that determines whether an item should be included in the new set.

Let’s see an example where we use an `if`

clause to filter even numbers from a set of integers.

**Example:**

numbers_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} even_numbers_set = {x for x in numbers_set if x % 2 == 0} print(even_numbers_set)

**Output:**

{2, 4, 6, 8, 10}

In this example, the `if x % 2 == 0`

condition checks whether a number is even using the reminder upon division operator `%`

. Only the even numbers from `numbers_set`

are included in `even_numbers_set`

. The result is the set `{2, 4, 6, 8, 10}`

.

## Using *if-else* Ternary Expressions

A ternary expression has a condition and assumes a different value depending on whether the condition is true or false. It’s called a ternary expression because it has three elements: the condition, an expression for when it’s true, and an expression for when it’s false.

The ternary expression is general syntax that fits within any expression context in Python, but it works very well in conjunction with set comprehensions to conditionally modify elements.

The syntax for a ternary expression within a set comprehension is:

{expression_if_true if condition else expression_if_false for item in iterable}

`condition`

: Determines which expression will be evaluated.`expression_if_true`

: Evaluated if the condition is true.`expression_if_true`

: Evaluated if the condition is false.

Let’s create a set of phrases indicating whether numbers in a list are even or odd using a ternary expression.

**Example:**

numbers = [1, 2, 2, 3, 3, 3, 4, 5] even_odd_indicators = {f"{x} is even" if x % 2 == 0 else f"{x} is odd" for x in numbers} print(even_odd_indicators)

**Output:**

{'2 is even', '3 is odd', '5 is odd', '1 is odd', '4 is even'}

In this example, the ternary operator controls whether the word “odd” or “even” appears at the end of each phrase. Note that the list being iterated over repeats the items `2`

and `3`

, but the resulting set contains only unique phrases. Additionally, the phrases do not appear to be in any particular order. This is because Python sets don’t guarantee the order of their member elements.

## Nested Set Comprehensions

Python also supports nested set comprehensions, allowing you to create sets from more complex, nested data structures. With nested set comprehensions, you can generate lists from iterables of iterables, such as lists of tuples.

The basic syntax of a nested set comprehension is as follows:

{expression for inner_iterable in outer_iterable for item in inner_iterable}

`expression`

: The expression that will be applied to each item.`inner_iterable`

: The variable that receives the inner iterables. For example, if you are processing a list of tuples, this variable will receive the individual tuples.`outer_iterable`

: This is the outer, overall iterable. If you are processing a list of tuples, this is the overall list itself.

The following example creates a set of unique elements from a 2-dimensional list (a.k.a. a list of lists or a matrix) using a nested set comprehension.

**Example:**

matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]] my_set = {x for row in matrix for x in row} print(my_set)

**Output:**

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

The set comprehension in the above example creates a set of the unique values that appear in the matrix.

As with nested list comprehensions, it is possible to have more levels of nesting and `if`

conditions for each level as well. For more information on how to do this with comprehensions, please see the *Nested List Comprehension* section of the list comprehension lesson.

## Summary & Reference for Python Set Comprehension

Set comprehensions are a powerful and concise way to create new sets by applying expressions to items in existing iterables.

The basic set comprehension syntax has curly braces `{}`

that surround an expression followed by `for`

loop-like syntax.

{expression for item in iterable}

Here is an example that generates a set of squares, eliminating duplicate values:

squares_set = {x**2 for x in range(-5, 6)} # squares_set --> {0, 1, 4, 9, 16, 25}

Set comprehensions can include `if`

clauses for filtering elements based on conditions.

numbers_set = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} even_numbers_set = {x for x in numbers_set if x % 2 == 0} # even_numbers_set --> {2, 4, 6, 8, 10}

Ternary expressions within set comprehensions allow for conditional element modification:

numbers = [1, 2, 2, 3, 3, 3, 4, 5] even_odd_indicators = {f"{x} is even" if x % 2 == 0 else f"{x} is odd" for x in numbers} # even_odd_indicators --> {'2 is even', '3 is odd', '5 is odd', '1 is odd', '4 is even'}

Python supports nested set comprehensions for creating sets from nested iterables:

matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]] my_set = {x for row in matrix for x in row} # my_set --> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

You can further nest set comprehensions with multiple levels and incorporate `if`

clauses at each level.