Exploring Set Comprehension in Python

Coding in Python will be made a lot easier with comprehension. That is the major reason for using comprehensions in python. It is a concise way to construct new sequences. Python generally supports four types of comprehension. They are list comprehension, set comprehension, dictionary comprehension, and generator comprehension.

Set comprehension in python is a succinct way of creating sets in Python. Using curly braces to notating the sets. For example, {y for in range(5)} creates a set of {1,2,3,4}. So this is the way of creating a set comprehension. This will reduce the complexity of the code.

What are the syntax, parameters, and return type of set comprehension?

Syntax

{expression(variable) for variable in input_set [predicate][, …]}

Parameters

  • expression, optional arguments
  • variable, required
  • input_set, required
  • predicate, optional
  • [,…]], optional

Returns

set

Benefits of set comprehension in Python

  • Reduce the length and complexity of the code.
  • It is more concise.
  • We can avoid loops and maps() while using set comprehension.

Set comprehension using a range

a={i for i in range(15)}
print(a)

Output

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}

Set comprehension using conditions

a={i for i in range(20) if i%2==0}
print(a)

Output

{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}

Set comprehension using math functions

a ={i*i for i in range(5)}
print(a)

Output

{0, 1, 4, 9, 16}

Code

new_set=set()
for i in range(10):
    if(i%2==0):
        new_set.add(i)
print(new_set)

Here we are declaring new_set=set. Then using for loop to iterate till the condition becomes false. Using a decision-making statement if to give the condition. The condition is that i%2==0.

So, it first takes the number zero and checks the condition, 0%2==0, so the condition is True. It will display zero. Next add(i) is for incrementing.

Now it takes one 1%2==0. The condition is False. It does not return one. And then it takes two likewise, it goes on till the condition becomes False. It does not take ten to check because we already know that the range function takes the value as n-1. So it checks till nine only.

Output

{0, 2, 4, 6, 8}

Nested set comprehension

A set that contains another set is known as nested set comprehension. Generally, if a set comes into another set in python, the inner set must be frozen set. Otherwise, it will throw an error to us. Set within a list or set within a dictionary does not require a frozen set.

Code

set={(i,j) for j in range(5,9) for i in range(8,12)}
print(set)
print(type(set))

Here we are going to set some range and get the numbers in that range. We are using the nested set here. First, giving the range(5,9) and the other range is (8,12). Using for loop to check the range.

Output

{(11, 7), (10, 5), (8, 8), (10, 8), (8, 7), (9, 6), (10, 7), (11, 6), (9, 5), (10, 6), (9, 8), (11, 5), (8, 6), (11, 8), (9, 7), (8, 5)}
<class 'set'>

Must Read | Tuple Comprehension in Python is it Possible?

Difference Between Set Comprehension and List Comprehension

Set ComprehensionList Comprehension
Using curly braces to create a set.Using a square bracket to create a list.
Doesn’t include duplicates.Includes a lot of duplicates.
Returns a setIt returns a list

1. What is set comprehension?

Set comprehension in python is a succinct way of creating sets in Python. Using curly braces to notating the sets.

2. Write a syntax for set comprehension.

{expression(variable) for variable in input_set [predicate][, …]}

3. What are the benefits of set comprehension?

Reduce the length and complexity of the code. It is more concise. We can avoid loops and maps() while using set comprehension.

4. What is mean by nested set comprehension?

A set that contains another set is known as nested set comprehension. Generally, if a set comes into another set in python, the inner set must be frozen set. Otherwise, it will throw an error to us.

5. Write a code to use set comprehensions in conditions?

a={i for i in range(20) if i%2==0}
print(a)

Conclusion

Here we have seen about set comprehension. It is beneficial. I hope you all know well about set comprehension. By using set comprehension, the code becomes user-friendly. It is a faster and best way to create sets.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments