Set Comprehension: A Compact Pathway to Pythonic Sets

You must have learned what sets in Python are, their syntax, and also their operations. In this article, you are going to the next step, which is creating a new set in a clear and concise way based on an iterable object. This concept is known as Set Comprehension. Let us understand more about this in the coming sections with an example program.

What is Set Comprehension in Python?

The concept of Set Comprehension is similar to List comprehension that is, to create new sets in an efficient way based on existing sets and this helps in combining everything in a single line of code.

With set comprehension, you can create sets based on specific criteria, perform transformations on elements or filter out unwanted elements.

Set comprehension offers a compact and expressive way to generate sets, reducing the need for explicit loops and conditional statements. It can greatly improve code readability and conciseness, making the code more elegant and efficient when dealing with set-based operations.

Syntax of Set Comprehension in Python:

The syntax for a set comprehension is as follows:

 >>> new_set = {expression for element in iterable if condition}

The breakdown of the syntax is as follows:

  • 'new_set': It is the name of the new set.
  • 'expression': It is an expression that is evaluated for each element in the iterable.
  • 'element': It is the current element in the iterable.
  • 'iterable': is an iterable object, such as a list, tuple, or string.
  • 'Condition': It is an expression that is evaluated for each element in the iterable. If the condition is True, the element is included in the new set.

Working of Set Comprehension in Python:

The working of set comprehension is quite simple and it is almost similar to list comprehension. This involves an iteration of an object and applying an expression to each item and optionally filtering the items based on specified conditions. The resulting elements are combined to form a set.

Here's a step-by-step breakdown of how set comprehension works:

  1. Define the syntax.
  2. Iterate over the iterable: The set comprehension begins by iterating over each item in the iterable.
  3. Apply the expression: For each item, the expression specified in the set comprehension is evaluated.
  4. Optional condition: If a condition is specified (using the if statement), it is evaluated for each item. The condition determines whether the item should be included in the resulting set.
  5. Collect the elements: As the set comprehension iterates over the iterable and evaluates the expression and condition, the resulting elements are collected.
  6. Form the set: Finally, the collected elements are combined to form a set. Duplicates are automatically removed, as sets cannot contain duplicate elements.

Example Program of Set Comprehension:

Let us see an example that can help us understand the concept of set comprehension:

Example:

 >>> numbers = [1, 2, 3, 4, 5]
 >>> # Create a set containing the squares of even numbers from the list
 >>> squares_of_evens = {x**2 for x in numbers if x % 2 == 0}
 >>> print(squares_of_evens)
Output:

{4,16}

In the example above, the set comprehension iterates over the numbers list. For each even number (if x % 2 == 0), the expression x**2 squares the number. The resulting elements, 4 and 16, are collected and combined to form the set {4, 16}.

Conclusion:

In conclusion, set comprehension in Python offers a powerful way to construct sets by combining the simplicity of set notation with the flexibility of comprehensions, you can create sets with transformed and filtered elements with a single line of code.

Do practice to get a stronghold of this concept and see you soon with another article and another interesting concept


Learn via Video Course

Python Logo

Python

6109

7 hrs