zaro

What is union symbol in Python?

Published in Python Sets 3 mins read

The union operation in Python for sets is primarily represented by the | (pipe) operator or performed using the .union() method. While the mathematical symbol for union is , Python provides its own specific syntax for this fundamental set operation.

The union is a fundamental operation that allows sets to be merged and connected to one another, resulting in a new set containing all unique elements from the original sets.


Understanding Union in Python Sets

In Python, sets are unordered collections of unique elements. Performing a union on sets means combining all distinct elements from two or more sets into a new set.

1. The | (Pipe) Operator

The | operator is a concise and commonly used way to perform a union between two or more sets in Python. It acts as a set union operator when used with set objects.

  • Syntax: set1 | set2 | set3
  • Behavior: Returns a new set containing all unique elements from set1, set2, and set3.

Example:

# Define two sets
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}

# Perform union using the | operator
union_set_operator = set_a | set_b
print(f"Union using | operator: {union_set_operator}")

# Union with multiple sets
set_c = {6, 7, 8}
union_multiple_sets = set_a | set_b | set_c
print(f"Union with multiple sets: {union_multiple_sets}")

Output:

Union using | operator: {1, 2, 3, 4, 5, 6}
Union with multiple sets: {1, 2, 3, 4, 5, 6, 7, 8}

2. The .union() Method

The .union() method is a set method that can be called on a set object, taking one or more iterables (like other sets, lists, or tuples) as arguments. It returns a new set containing all unique elements.

  • Syntax: set1.union(set2, set3, ...)
  • Behavior: Returns a new set containing all unique elements from set1 and all the sets/iterables passed as arguments.

Example:

# Define two sets
set_x = {'apple', 'banana', 'cherry'}
set_y = {'banana', 'orange', 'grape'}

# Perform union using the .union() method
union_set_method = set_x.union(set_y)
print(f"Union using .union() method: {union_set_method}")

# Union with multiple sets/iterables
list_z = ['grape', 'kiwi', 'apple']
union_multiple_args = set_x.union(set_y, list_z)
print(f"Union with multiple arguments: {union_multiple_args}")

Output:

Union using .union() method: {'apple', 'banana', 'cherry', 'orange', 'grape'}
Union with multiple arguments: {'apple', 'banana', 'cherry', 'orange', 'grape', 'kiwi'}

Comparison: | Operator vs. .union() Method

Both the | operator and the .union() method achieve the same result for set unions. However, there are subtle differences:

| Feature | | (Pipe) Operator | .union() Method |
| :------------------ | :---------------------------------------- | :------------------------------------------------- |
| Readability | Often more concise for two sets. | More explicit, especially with multiple arguments. |
| Argument Types | Requires all operands to be sets. | Accepts any iterable (sets, lists, tuples, etc.). |
| Conciseness | Good for chained operations (s1 | s2 | s3). | Verbose for multiple arguments (s1.union(s2, s3)). |


Practical Insights and Use Cases

  • Combining Data: Union is useful when you need to merge data from different sources while ensuring all unique entries are present.
  • Deduplication: Since sets automatically handle unique elements, performing a union is a natural way to combine lists or other collections and remove duplicates.
  • Filtering: When you need a superset of elements that meet criteria from different filters.

When to use which:

  • Use the | operator for a clean and concise union of two or more set objects. It's generally preferred for its readability when dealing exclusively with sets.
  • Use the .union() method when you need to combine a set with other iterable types (like lists or tuples) without converting them to sets first. It offers more flexibility in argument types.

Ultimately, both are effective tools for performing set union in Python, and the choice often comes down to personal preference, context, and the type of arguments being combined.