Python Set Comprehension Tutorial

In this section, we will learn what the Set Comprehension is and how to use it in Python.

What is Set Comprehension in Python?

Python Set Comprehension is a method of creating a Set object based on the elements of an iterable object.

You might be asking the `set()` function does the same thing and so what’s the different between a Set Comprehension and the `set()` function then?

Well, Set Comprehension allows us to run filters on the elements of the target iterable object and also modify the incoming elements before they are being part of the new Set object. And all of these happen in just one single line of code.

Basically, the Set Comprehension is a combination of `for in` loop, as well as an `if` statement and another expression! So three separate statements are combined and created the Set Comprehension.

Python Set Comprehension Syntax:

newSet = {expression for item in iterable if condition == True}

`newSet`: this is final Set that will be produced after the operation of the Set comprehension is done.

`for item in iterable`: this is a simple for loop that will go through the items of the target iterable object (like a List or a Set object etc.). Here the `iterable` is the target iterable object. The `item` is the returned element from the iterable object per each iteration. We put `in` between the `item` and `iterable` in order to separate them. And of course the for loop starts with the keyword `for`.

` if condition == True`: After each iteration in the loop, the returned `item` will be passed to this optional if condition. Here we can run a condition on top of the item and if it passed, it will be passed to the `expression` part of the Set comprehension! Remember, this if condition is optional and can be ignored.

For example, the condition could be as simple as (if item not in [“Jack”,”Omid”,”Elon”]) meaning as long as the returned item from the target Iterable object is not one of these mentioned elements, then it can be passed to the `expression` to become part of the new Set.

`expression`: expression can be as simple as a variable name like `item` for example! This expression represents the item that will be passed as part of the elements of the new Set. We say it’s an expression because we are allowed to modify the item before it becomes part of the new Set! For example, if the new elements are all of type number, and we need to multiply them by the number 10 before they are being added to the Set, then we can put the expression right here as `item * 10`.

Example: using python Set comprehension with if statement

list1 = ["Ellen","Jack", "Omid",False,"Elon", 20.3, "James","Richard","Jeremy", "Ian","Elon", "Kimberly"]

ignoreList = ["Ellen","Omid","Elon"]

set1 = {item for item in list1 if item not in ignoreList}

print(set1)

Output:

{False, 'Kimberly', 'Jack', 'Richard', 20.3, 'Ian', 'James', 'Jeremy'}

Let’s dissect the Set Comprehension of this example:

The for loop here is: `for item in list1` which means we want to take the elements of the `list1` and use them as the elements of the new Set object.

The condition is: `if item not in ignoreList`. This condition is saying check the incoming items from the `list1` object and as long as they are not either of the elements in the `ignoreList`, then they can pass and become part of the new Set object.

`item`: The expression of this example is simply the new item itself without any sort of modifications.

Leave a Reply