6 Easy Ways to Combine Two Sets in Python

0
33
6 Easy Ways to Combine Two Sets in Python

Combing sets can be helpful because they answer complex questions and compares cohorts of your data.

What is Set?

A set is an unordered collection of elements. You can perform mathematical operations like union, intersection, etc.

How to create a Set?

To create a Set in Python, use either the set() function or curly braces({ }).

How to Combine Two Sets in Python

To join two Sets in Python,

  1. Use set.union(): It returns a new set containing combining elements.
  2. Using set.update(): It will only update the value of the set by calling the update() method 
  3. Use functools.reduce(): It applies a function to iterables to reduce them into a single value.
  4. Using itertools.chain(): It is used to chain multiple iterables and returns a single sequence.
  5. Using the “|” operator: Combines two or more sets in python using bitwise or operator (|)
  6. Using unpack operator(*): The asterisk (*) is the unpacking operator. It unpacks values from iterable objects. 

Set.union() method

The union() is a built-in method that returns a new set containing all elements from both sets.

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_c = set_a.union(set_b)
print(set_c)

Output

{'Xbox Series X', 'Playstation', 500, 550, 300, 'Xbox Series S'}

You can see that elements from both sets are combined, and it returns the third set, the combination of both sets.

Using Set.update() method

The update() is a built-in Python method that updates the current set by adding elements from another set. It removes duplicate elements.

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_a.update(set_b)
print(set_a)

Output

{'Xbox Series S', 'Xbox Series X', 500, 550, 'Playstation', 300}

You can see that set_b remains unchanged, whereas set_a gets updated with the combined values.

Using reduce() Method

The reduce() method is the functools package’s method that is used to apply a function to iterables to reduce it into a single value. Use the reduce() method and operator.or_ to combine two sets.

from functools import reduce
import operator

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_c = reduce(operator.or_, [set_a, set_b])
print(set_c)

Output

{500, 'Playstation', 'Xbox Series X', 550, 300, 'Xbox Series S'}

The reduce() will return the bitwise operation of set_a and set_b. Operator.or_ shall work similarly to the union operation.

Using itertools.chain() method

The itertools.chain() method chains multiple iterables and returns a single sequence.

from itertools import chain

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_c = chain(set_a, set_b)
print(set(set_c))

Output

{'Xbox Series S', 550, 'Xbox Series X', 300, 500, 'Playstation'}

The chain() method returns the itertools.chain object, and we can convert that object to a set using the set() method.

Using the “|” OR operator

The bitwise OR operator combines two or more sets in Python.

from itertools import chain

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_c = set_a | set_b
print(set(set_c))

Output

{'Playstation', 'Xbox Series X', 500, 550, 'Xbox Series S', 300}

The bitwise operator is equivalent to the mathematical union operation combining two or more sets.

Using the unpacking operator (*)

To unpack a set in Python, use the asterisk operator. The unpack operator unpacks values from iterable objects.

from itertools import chain

set_a = {"Playstation", "Xbox Series X", "Xbox Series S"}
set_b = {550, 500, 300}

set_c = {*set_a, *set_b}
print(set(set_c))

Output

{500, 'Xbox Series X', 550, 'Xbox Series S', 300, 'Playstation'}

Conclusion

The best way to combine two sets in Python, use the set.union().

That’s it.

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.