Python Union
Table of Contents
In the last lesson, we learned about Python Set Operations. Now let’s look more closely at union and combine unique items from different sets.
Let’s Talk About Union in Detail
We already saw a quick example of union in the last article. Now let’s go deeper.
Union is one of the most common set operations. It combines two sets into one. All items from both sets. No duplicates. That’s it.
Think of it like two guest lists for a party. One list has our friends. The other list has our cousin’s friends. The union is everyone who is invited. If someone is on both lists, they only appear once.
The Union Operator | in Python
The simplest way to take the union of two sets is with the | operator.
set1 = {1, 2, 3}set2 = {3, 4, 5}result = set1 | set2print(result)Output
{1, 2, 3, 4, 5}Notice that 3 appears in both sets, but in the result it appears only once. That’s because sets don’t allow duplicates.
We can chain multiple unions.
set1 = {1, 2}set2 = {2, 3}set3 = {3, 4}result = set1 | set2 | set3print(result)Output
{1, 2, 3, 4}It works from left to right. So it’s like (set1 | set2) | set3. But the result is the same.
The union() Method in Python
We can also use the .union() method. It does the same thing.
set1 = {1, 2, 3}set2 = {3, 4, 5}result = set1.union(set2)print(result)Output
{1, 2, 3, 4, 5}The union() method can take multiple arguments.
set1 = {1, 2}set2 = {2, 3}set3 = {3, 4}result = set1.union(set2, set3)print(result)Output
{1, 2, 3, 4}So we can pass as many sets as we want.
Union with Other Iterables in Python
Here’s something nice about the union() method. It can take any iterable, not just sets. We can pass a list, a tuple, or even a string.
set1 = {1, 2, 3}list1 = [3, 4, 5]result = set1.union(list1)print(result)Output
{1, 2, 3, 4, 5}The list is converted to a set internally, and then the union is performed.
But the | operator only works with sets. We cannot use it with a list.
set1 = {1, 2, 3}list1 = [3, 4, 5]result = set1 | list1 # Error!Output
TypeError: unsupported operand type(s) for |: 'set' and 'list'So if we have a list and want to union it with a set, we use the .union() method.
In-Place Union with update() in Python
The | operator and .union() method create a new set. The original sets are unchanged.
If we want to modify a set in place, we use the .update() method.
set1 = {1, 2, 3}set2 = {3, 4, 5}set1.update(set2)print(set1)Output
{1, 2, 3, 4, 5}Now set1 has been changed. It contains all items from both sets. The original set1 is gone.
We can also update with multiple sets.
set1 = {1, 2}set2 = {2, 3}set3 = {3, 4}set1.update(set2, set3)print(set1)Output
{1, 2, 3, 4}And update() also accepts any iterable.
set1 = {1, 2}set1.update([3, 4])print(set1)Output
{1, 2, 3, 4}Python Union: A Real-Life Example
Let’s say we have two lists of email addresses. One from our newsletter signup. One from a giveaway. We want to send an email to everyone. But we don’t want to send duplicates.
newsletter = {"alex@example.com", "sam@example.com", "john@example.com"}giveaway = {"sam@example.com", "emma@example.com", "david@example.com"}
all_emails = newsletter | giveawayprint(all_emails)Output
{'alex@example.com', 'sam@example.com', 'john@example.com', 'emma@example.com', 'david@example.com'}Now we can send one email to each address. No duplicates.
Another Example — Combining Tags
post1_tags = {"python", "coding", "beginner"}post2_tags = {"python", "advanced", "tips"}
all_tags = post1_tags.union(post2_tags)print(all_tags)Output
{'python', 'coding', 'beginner', 'advanced', 'tips'}That’s useful for finding all tags used across multiple posts.
Union with an Empty Set
Union with an empty set does nothing. It just returns a copy of the other set.
set1 = {1, 2, 3}empty = set()result = set1 | emptyprint(result)Output
{1, 2, 3}That’s expected.
Common Mistakes with Python Union
Mistake 1: Using | with a List
set1 = {1, 2}list1 = [2, 3]result = set1 | list1 # Error!So what do we do? We use the .union() method.
result = set1.union(list1)Mistake 2: Thinking Union Modifies the Original
set1 = {1, 2}set2 = {2, 3}set1 | set2print(set1) # {1, 2}The | operator returns a new set. It doesn’t change set1.
If we want to change set1, we use set1.update(set2) or set1 |= set2.
Mistake 3: Forgetting That Union Removes Duplicates
set1 = {1, 2, 2, 3}set2 = {3, 4, 4, 5}result = set1 | set2print(result)Output
{1, 2, 3, 4, 5}Duplicates are automatically removed. That’s the point of sets.
Mistake 4: Using Union on Non-Set Types Incorrectly
result = [1, 2] | [2, 3] # Error!The | operator doesn’t work on lists. We convert to sets first.
result = set([1, 2]) | set([2, 3])print(result)Output
{1, 2, 3}Next, we will learn about Python Set Intersection and find items shared by multiple sets.