Python Tutorial

Python Set union() Method with Examples

TGTeam GyaniPanditNovember 14, 20263 min read

Hello friends! Suppose you have two lists of names and you want one combined list with no name repeated. In Python, sets make this very easy. The union() method combines two or more sets and returns a new set containing every item that appears in any of them. Since sets never hold duplicates, an item that appears in more than one set is kept only once. The original sets are not changed. Let’s see how it works.

set1.union(set2, set3, ...)
set1 | set2 | set3        # operator form

Basic Example

a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b))
print(a | b)
print(a)

Output:

{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5}
{1, 2, 3}

The common item 3 appears only once in the result, and a is unchanged.

And you are not limited to just two sets.

Union of More Than Two Sets

c = {5, 6}
print(a.union(b, c))
print(a | b | c)

Output:

{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6}

union() vs the | Operator

You may have seen the | symbol used for the same job. They give the same result, with one difference: union() accepts any iterable (list, tuple, string, range), while | needs sets on both sides.

print(a.union([3, 7, 7], (8,)))

try:
    print(a | [3, 7])
except TypeError as e:
    print("TypeError:", e)

Output:

{1, 2, 3, 7, 8}
TypeError: unsupported operand type(s) for |: 'set' and 'list'

Changing a Set in Place: update() and |=

So far, union() has always given us a new set. But what if you want to add the items to an existing set instead of creating a new one, use update() or |=:

s = {1, 2}
s.update([2, 3], {4})
print(s)
s |= {5}
print(s)

Output:

{1, 2, 3, 4}
{1, 2, 3, 4, 5}

Read more: Python set update()

Now let’s use all this in a small real-life example.

Practical Example: All Students in Two Batches

morning = {"Riya", "Aman", "Kabir"}
evening = {"Aman", "Neha", "Riya", "Zoya"}
all_students = morning | evening
print(len(all_students), sorted(all_students))

Output:

5 ['Aman', 'Kabir', 'Neha', 'Riya', 'Zoya']

Students who attend both batches are counted once. Note that a set has no fixed order, so sorted() is used to print the names in a predictable order.

Key Points

Let’s quickly revise what we learned:

  • union() always returns a new set, even when called with no arguments (it then returns a copy).
  • Duplicates are removed automatically.
  • The order of items in a set is not guaranteed, so never rely on it.
  • Use update() or |= to change the original set instead of creating a new one.

Practice Questions

Try these yourself before you look at the answers:

  1. print({1, 2} | {2, 3} | {3, 4})
  2. print(len({"a", "b"} | set("abc")))

Answers: 1) {1, 2, 3, 4}. 2) 3, because set("abc") is {'a', 'b', 'c'} and the union has three distinct letters.

So friends, remember the main points: union() does not change the original set, it returns a new one, and to modify the original you use update() or |=. It also accepts any iterable, such as a list, while the | operator needs sets on both sides. And while union gives you items present in any of the sets, intersection gives only the items present in all of them. Try the examples in your Python editor and you will get comfortable with sets in no time.

More set methods: Set methods in Python · intersection() · difference() · add()

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses