Horje
Join Two Sets In One Line Without Using "|" in Python

In this article, we will understand how to join two sets in one line without using “|”.As we know when working with sets in Python, there might be scenarios where you need to combine two sets into a single set. The easiest way to achieve this is by using the union operator (|). However, we need to join two sets in one line without using “|”. So in this article, we will explore different methods to join two sets without using the | operator.

How To Join Two Sets In One Line Without Using “|”?

Below, are the ways through which we can Join Two Sets In One Line Without Using “|” in Python.

Join Two Sets In One Line Using set() Method

In this example, the below code combines two sets, set1 and set2, by converting them to lists, extending one list with the other, and then converting the result back into a set using the `set()` function. The resulting set, contains elements from both original sets.

Python3

set1 = {1, 2, 3}
set2 = {3, 4, 5}
  
# Using set() to convert the extended list back into a set
result_set = set(list(set1) + list(set2))
  
# Displaying the result
print(result_set)

Output

{1, 2, 3, 4, 5}



Join Two Sets In One Line Using update() Method

In this example, below code utilizes the `update()` method to merge elements from set2 into set1, effectively joining the two sets. The resulting set, now containing elements from both original sets, is displayed using the `print()` function.

Python3

set1 = {1, 2, 3}
set2 = {3, 4, 5}
  
# Using update() to add elements from set2 to set1
set1.update(set2)
  
# Displaying the result
print(set1)

Output

{1, 2, 3, 4, 5}



Join Two Sets In One Line Using the * operator

In this example, below code employs the * operator to unpack elements from both sets, creating a new set using the set() constructor. The resulting set, containing elements from both original sets, is stored in the variable “result_set” and printed.

Python3

#Examples
set1 = {1, 2, 3}
set2 = {3, 4, 5}
  
# Using set() constructor with the * operator to create a new set
result_set = set((*set1, *set2))
  
# Displaying the result
print(result_set)

Output

{1, 2, 3, 4, 5}



Join Two Sets In One Line Using itertools.chain() Method

In this example, below code utilizes the `itertools.chain()` function to concatenate elements from both sets, and then a new set is created using the set() constructor. The resulting set, containing elements from both original sets, is stored in the variable “result_set” and printed.

Python3

from itertools import chain
# Examples
set1 = {1, 2, 3}
set2 = {3, 4, 5}
  
# Using itertools.chain() and set() constructor
result_set = set(chain(set1, set2))
  
# Displaying the result
print(result_set)  # Output: {1, 2, 3, 4, 5}

Output

{1, 2, 3, 4, 5}



Conclusion

In conclusion, when seeking to join two sets in Python without using the union operator (|), several methods offer concise and efficient solutions. Whether through list conversion and set creation, leveraging set methods like update() or symmetric_difference(), or using itertools.chain(), these alternatives provide flexibility and clarity for combining sets in a single line of code. Choosing the appropriate method depends on the specific requirements of the task at hand, offering diverse approaches for set manipulation in Python.




Reffered: https://www.geeksforgeeks.org


Python

Related
Slicing with Negative Numbers in Python Slicing with Negative Numbers in Python
Append Tuple To A Set With Tuples Append Tuple To A Set With Tuples
Get Length of a List in Python Without Using Len() Get Length of a List in Python Without Using Len()
Count the Key from Nested Dictionary in Python Count the Key from Nested Dictionary in Python
Divide Two Integers to get Float in Python Divide Two Integers to get Float in Python

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13