Python Set Comprehension Examples

Spread the love

Set comprehension is a concise way to create a new set based on existing iterable(s). It is similar to a but returns a set instead of a list. In this article, we will discuss Set comprehension and how to create sets by using different data structures in Python.

Python Sets in python is a one-dimensional data structure that will not allow duplicates. Set comprehension is also a data structure that will store the data by filtering the elements using conditions/expressions. So the set comprehension will take an iterator to iterate the elements from other data structures using an expression/condition.

2. Create a Python Set from Comprehension

By using set comprehension you can create sets from the Python sequence type, Here, the sequence includes data structures like sets, lists, and tuples.

2.1 Syntax

Following is the syntax of the set comprehension.


# Syntax
{iterator  for iterator in sequence}

Here, iterator is used to iterate the elements from other data structure and place in the set.

3. Create a Set from List using Comprehension

Here, I create a list with string values and use this with comprehension to create Python sets. The following example creates a set with all elements from the list.


# Consider 4 strings in a list
list1 = ["wecome","to","sparkby","examples"]
print("List : ",list1)

# Create set from the list
converted = {i  for i in list1}
print("Set: ",converted)

# Output:
# List :  ['wecome', 'to', 'sparkby', 'examples']
# Set:  {'wecome', 'examples', 'sparkby', 'to'}

4. Create Set from Tuple using Set Comprehension

Here, let’s create a set from the tuple using the set comprehension.


# Consider 8 strings in a tuple
tuple1 = ("wecome","to","sparkby","examples","wecome","to","sparkby","examples")
print("Tuple : ",tuple1)

# Create set from the tuple
converted = {i  for i in tuple1}
print("Set: ",converted)

# Output:
# Tuple :  ('wecome', 'to', 'sparkby', 'examples', 'wecome', 'to', 'sparkby', 'examples')
# Set:  {'to', 'sparkby', 'examples', 'wecome'}

First we created a tuple that hold 8 strings with duplicates, Now we are passing each element one by one in the tuple to the Set using the set comprehension. As we know that Set will not allow duplicates, there ate 4 duplicates in the tuple, so set won’t included them. Hence the set holds only 4 elements.

5. Create Set from Existing Set using Set Comprehension

Here, first, we created a set that holds 9 integers, and we used it to create the new set from the existing set by subtracting 3 from each element in the list by using python set comprehension.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Create set by substracting
set_comprehension = {i-3 for i in myset}
print(set_comprehension)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# {0, 64, 3, 9, 42, 53, 86, -2, 31}

6. Filter the Set using Set Comprehension

Till now, we have seen how to create the set using set comprehension. Let’s see how to filter the elements in existing set using the Set Comprehension.

6.1 Syntax

Following is the syntax, here, conditions are used to filter the elements in the existing set and return the new set.


# Syntax
{iterator  for iterator in sequence conditions}

6.2 Examples of Filtering the Set using the Set Comprehension

Below I have converted several python set comprehension examples that filter the elements from the existing and create a new set.

Example 1

Here, We create a set with some integers and filter the elements that are greater than 25 using set comprehension by specifying the condition inside it.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Create set by filtering greater than 25 using set comprehension.
set_comprehension = {i for i in myset if i > 25}
print(set_comprehension)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# {34, 67, 45, 56, 89}

Example 2

We have a set with some integers, Now we created a set that hold elements that are less than 25 using set comprehension by specifying the condition inside it. Finally the filtered set hold only 4 elements.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Create set by filtering less than 25 using set comprehension.
set_comprehension = {i for i in myset if i < 25}
print(set_comprehension)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# {1, 3, 12, 6}

Example 3

Here, we used set comprehension to convert the data type of all elements present in the set to string. After we are displaying each element type.


# Consider the set with some integers
myset = {12,34,56}
print("Actual set:",myset)

# Convert all the elements in the set to strings.
set_comprehension = {str(i) for i in myset}
for i in set_comprehension:
  print(type(i))

# Output:
# Actual set: {56, 34, 12}
# 
# 
# 

4. Conclusion

In this article, you have learned what is set comprehension and how to create set from a list, tuple, and set with different examples. It can also be possible to filter the elements from the existing set using set comprehension.

Happy learning!!

Leave a Reply

You are currently viewing Python Set Comprehension Examples