Python Set union() Method [4 Examples]

In this Python article, I will explain what the Python set union() method is, its syntax, parameters, and return values. I will explain what the | operator is in Python. And also many different scenarios that will elaborate on the Python set union() method in detail.

To perform set union operations in Python, one can use either the union() method or the | operator. Both effectively merge multiple sets into a single set, ensuring all elements are unique. The union() method allows combining multiple sets (e.g., set1.union(set2, set3)) and is more flexible, while the | operator offers a concise way to unite two sets (e.g., set1 | set2), with both methods automatically removing any duplicates.

Python Set union() Method

In Python, a set is a collection of unique elements. The union operation combines all elements from the involved sets, removing any duplicates, and resulting in a new set containing every distinct element.

Syntax of Union() function in Python

The Python Set union() method can be performed in two ways:

  • Using the union() method:
set1.union(set2, set3, ...)
  • Using the | operator:
set1 | set2 | set3 | ...

Parameters of Python set union

set1, set2, set3, …: These are the Python sets to be united. While set1 is the set on which the method is called, set2, set3, and so on are other sets that are combined with set1 through Python.

The return value of the Python set union() function

The Python set union function returns a new set, which consists of all unique elements from the sets involved in the operation.

Union() Function in Python Use Cases

Let’s see some different scenarios where we can use the union() function in Python.

Case 1: Python set union function

Here, we are directly using the Python set union() function to find the union of the sets through Python.

east_coast_states = {'New York', 'Florida', 'Virginia', 'Maine'}
midwest_states = {'Illinois', 'Michigan', 'Ohio', 'Indiana'}

combined_states = east_coast_states.union(midwest_states)
print(combined_states)

Output: The union() method combines the two sets into a new set in Python.

{'Michigan', 'Indiana', 'New York', 'Florida', 'Virginia', 'Illinois', 'Ohio', 'Maine'}
Python Set union() Method

The basic use of Python set union() method.

Case 2: The union of more than two sets in Python

Let’s consider a scenario where we have to use the union operation to combine more than two sets in Python.

teen_favorites = {'New York', 'Los Angeles', 'Chicago', 'Miami'}
young_adult_favorites = {'San Francisco', 'New York', 'Seattle', 'Austin'}
senior_favorites = {'Miami', 'Phoenix', 'San Francisco'}

combined_favorites = teen_favorites.union(young_adult_favorites, senior_favorites)
print(combined_favorites)

Output: Here, is how we can find the union of three sets through Python.

{'Austin', 'Phoenix', 'Los Angeles', 'San Francisco', 'Chicago', 'Seattle', 'New York', 'Miami'}
Python Set union() function

This way we can use the Python set union() method to find the union of more than three sets.

Case 3: Union() function in Python on strings

The Python set union() function can also be used with strings in Python, treating each string as a set of characters. When applied to strings, the union operation combines the characters from each string, removing duplicates.

california = 'CA'
new_york = 'NY'
florida = 'FL'

unique_characters = set(california).union(new_york, florida)
print(unique_characters)

Output: Each variable is treated as a set of its constituent characters. The union() function combines these character sets, ensuring that each character is represented only once in the resulting set.

{'Y', 'F', 'L', 'N', 'C', 'A'}
Union() function in Python

This way we can use the union() in Python for string.

Case 4: Python set union (|) operator

The | operator in Python merges the elements of the involved sets into a new set, ensuring all elements are unique. This operation is particularly useful for merging sets without duplicates.

teenagers = {'Disney World', 'Universal Studios', 'Times Square'}
families = {'Disney World', 'Grand Canyon', 'Statue of Liberty'}
retirees = {'Grand Canyon', 'Statue of Liberty'}

combined_choices = teenagers | families | retirees
print(combined_choices)

Output: Here, we have used the | operator rather than the Python set union() method.

{'Times Square', 'Statue of Liberty', 'Grand Canyon', 'Disney World', 'Universal Studios'}
Python Set Union() Method and Operator

This way we can use the | operator to find the union of sets in Python.

Conclusion

Both the Python set union() method and the | operator provide efficient and effective ways to perform set union operations, catering to different coding styles and needs. Whether we require the flexibility of combining multiple sets at once or the simplicity of a concise syntax, these tools ensure that the resultant set is a unique amalgamation of all elements from the involved sets, free from duplicates.

With the help of the above example, we can easily understand the method.

You may also like to read: