Introduction

In Python programming, a set is an unordered collection of unique elements. Set comprehension is a concise way of creating a set in Python using a single line of code. It allows you to create complex sets with ease and simplicity. This tutorial will walk you through the basics of set comprehension, including how to use it, what it is used for, and why it is useful in Python programming. By the end of this tutorial, you will have a solid understanding of how to use set comprehension in your Python projects. Let's get started!

Table of Contents :

  • Set comprehension
    • using loops
    • using map() function
    • using set comprehensions
  • Usage of set comprehensions

Set comprehension :

  • Sometimes we need to carry on some operations on set elements.
  • Different ways of doing this are :
    • using loops
    • using map() function
    • using set comprehensions

Using Python loops : 

  • Control flow of using any python loop for transforming elements of sets : 
    • iterate over all elements of the sets
    • perform the operations on each element
    • create a new set from the transformed element.
  • The order of the transformed elements may not be the same in the resultant new set.
  • Code Sample : 

my_set = {1, 2, 3, 4}

sqr_set = set()
for i in my_set:
    sqr = i ** 2
    sqr_set.add(sqr)

print(f"Value of my_set = {my_set}")
print(f"Value of sqr_set = {sqr_set}")

# Output
# Value of my_set = {1, 2, 3, 4}
# Value of sqr_set = {16, 1, 4, 9}


using map() function :

  • We can use the  map()  function to get the same result.
  • The  map()  function can make use of lambda expression to transform elements of set.
  • The  map()  function returns a map object
  • The  set()  function should be used to convert map object back to set.
  • Code Sample : 

my_set = {1, 2, 3, 4}

sqr_set_obj = map(lambda x: x ** 2, my_set)
sqr_set = set(sqr_set_obj)

print(f"Value of my_set = {my_set}")
print(f"Value of sqr_set = {sqr_set}")

# Output
# Value of my_set = {1, 2, 3, 4}
# Value of sqr_set = {16, 1, 4, 9}


Set Comprehensions :

  • Set comprehension is a refined and sophisticated way of carrying on basic operations on Set elements.
  • Control flow of Set comprehensions is as follows : 
    • The interpreter loops over all the elements of the Set.
    • performs some action (like filtering or modifying the value) on each element.
    • creates and returns a new Set.
    • The original set remains unaltered.
  • The basic syntax for Set comprehensions is :   {x for x in d} 
  • Code Sample : 

my_set = {1, 2, 3, 4}
sqr_set = {x**2 for x in my_set}

print(f"Value of my_set = {my_set}")
print(f"Value of sqr_set = {sqr_set}")

# Output
# Value of my_set = {1, 2, 3, 4}
# Value of sqr_set = {16, 1, 4, 9}


Usage of set comprehensions :

  • We can use Set comprehensions for various purposes.
  • A couple of usages of Set comprehensions are : 
    • to transform Set elements
    • to filter Set elements.

Using Set Comprehensions to transform elements :

  • We can use set comprehensions to transform elements of a set and create a new set with transformed elements.
  • Code Sample : 

my_set = {1, 2, 3, 4}
sqr_set = {x**2 for x in my_set}

print(f"Value of my_set = {my_set}")
print(f"Value of sqr_set = {sqr_set}")

# Output
# Value of my_set = {1, 2, 3, 4}
# Value of sqr_set = {16, 1, 4, 9}

Using Set Comprehensions to filter out elements :

  • We can use set comprehensions to filter out elements of a set and create a new set with filtered elements.
  • Code Sample : 

my_set = {10, 20, 30, 40, 50}
new_set = {x for x in my_set if x > 20}

print(f"Value of my_set = {my_set}")
print(f"Value of new_set = {new_set}")

# Output
# Value of my_set = {50, 20, 40, 10, 30}
# Value of new_set = {40, 50, 30}


Prev. Tutorial : Disjoint Sets

Next Tutorial : String operators