How to merge two sets in Python
Learn how to merge two sets in Python using various methods. Discover tips, real-world applications, and how to debug common errors.
.png)
The combination of sets in Python is a common data manipulation task. Python offers efficient methods, like the union() method or the | operator, to merge unique elements from different collections.
In this article, you'll explore various techniques to merge sets. You'll also find practical tips, real-world applications, and debugging advice to help you master this essential skill for your projects.
Using the | operator
set1 = {1, 2, 3}
set2 = {3, 4, 5}
merged_set = set1 | set2
print(merged_set)--OUTPUT--{1, 2, 3, 4, 5}
The pipe operator | offers a concise and highly readable way to perform a set union. It functions just like the mathematical union symbol, merging all elements from set1 and set2 into a new set.
The resulting merged_set contains every unique item from the original sets. Notice the value 3, present in both, appears only once in the output. Sets inherently enforce uniqueness, so this operation is an efficient way to combine collections while automatically handling deduplication.
Basic techniques for merging sets
Beyond the | operator, Python offers dedicated methods that give you more control, allowing you to either create a new combined set or modify one directly.
Using the union() method
set1 = {1, 2, 3}
set2 = {3, 4, 5}
merged_set = set1.union(set2)
print(merged_set)--OUTPUT--{1, 2, 3, 4, 5}
The union() method offers a more explicit way to merge sets. When you call set1.union(set2), it returns a completely new set containing all unique elements from both, leaving the original sets untouched.
- This method is functionally identical to using the
|operator for merging two sets. - Its main advantage is versatility. The
union()method can accept any iterable—like a list or tuple—not just a set, as its argument.
Using the update() method to modify in-place
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)--OUTPUT--{1, 2, 3, 4, 5}
If you need to modify a set directly instead of creating a new one, the update() method is your tool. It merges elements from another collection right into the existing set, which can be more memory-efficient for large datasets.
- Notice how
set1itself is changed after the operation. - The method doesn't return a new set—it returns
None. - Like
union(),update()also works with any iterable, such as a list or tuple.
Using the |= operator for in-place union
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1 |= set2
print(set1)--OUTPUT--{1, 2, 3, 4, 5}
The |= operator is an augmented assignment operator that provides a concise way to perform an in-place union. It functions as a shorthand for the update() method, directly modifying the set on the left side of the operator.
- When you use
set1 |= set2, you're telling Python to add all unique elements fromset2intoset1. - This approach is memory-efficient because it avoids creating an entirely new set, making it a great choice when you simply need to update an existing collection.
Advanced techniques for merging sets
Building on the basics, you can tackle more complex scenarios by merging multiple sets at once using powerful operators and specialized functions.
Merging multiple sets at once
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}
merged_set = set1.union(set2, set3)
print(merged_set)--OUTPUT--{1, 2, 3, 4, 5, 6, 7}
The union() method isn't limited to just two sets. You can efficiently merge several collections at once by passing them as multiple arguments. This command gathers all unique elements from every set you provide, creating a single, comprehensive new set.
- Just as the example shows, you can pass multiple sets like
set1.union(set2, set3). - The
|operator also supports this, allowing you to chain unions likeset1 | set2 | set3for a more concise syntax. - Both methods return a brand new set, leaving your original sets completely untouched.
Using functools.reduce with multiple sets
import functools
set1 = {1, 2}
set2 = {2, 3}
set3 = {3, 4}
merged_set = functools.reduce(lambda x, y: x | y, [set1, set2, set3])
print(merged_set)--OUTPUT--{1, 2, 3, 4}
For a more functional programming approach, you can use functools.reduce. This is especially powerful when you have a dynamic list of sets to merge and need to boil them down to a single result.
- The
reducefunction works by applying an operation cumulatively to the items in a sequence. - In this case, the
lambda x, y: x | yfunction tells it to repeatedly perform a union. - It starts by merging the first two sets, then merges that result with the third, and so on, until all sets are combined.
Using unpacking to merge sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}
merged_set = {*set1, *set2, *set3}
print(merged_set)--OUTPUT--{1, 2, 3, 4, 5, 6, 7}
The unpacking operator, denoted by an asterisk *, offers a visually clean and modern way to merge sets. You can create a new set literal and unpack the contents of your original sets directly into it. This approach is often favored for its straightforward syntax.
- The expression
{*set1, *set2, *set3}tells Python to take all elements from each set. - The surrounding curly braces
{}then gather these elements into a single new set. - Duplicate values are automatically discarded during this process, just as with the
union()method.
Move faster with Replit
Replit is an AI-powered development platform that transforms natural language into working applications. Describe what you want to build, and Replit Agent creates it—complete with databases, APIs, and deployment.
For the set merging techniques we've explored, like using the union() method, Replit Agent can turn them into production-ready tools. You can describe an application, and the agent will handle the implementation.
- Build a user management tool that combines permission sets from different roles to create a comprehensive access list.
- Create a contact aggregator that merges follower lists from multiple social platforms to identify unique users.
- Deploy a data cleaning utility that deduplicates customer records from separate databases by merging their unique identifiers.
Turn your concepts into working software. Describe your app idea and let Replit Agent write the code, test it, and fix issues automatically, all within your browser.
Common errors and challenges
While merging sets is usually straightforward, a few common pitfalls can trip you up, from type errors to unexpected behavior with in-place methods.
Fixing TypeError when using the | operator with non-set objects
A frequent issue is the TypeError that appears when you try using the | operator with a set and a non-set, like a list. This operator is strict—it demands that both operands be sets.
- To fix this, you can either convert the list to a set before the operation or use the more flexible
union()method, which accepts any iterable.
Avoiding common mistakes with the update() method
A classic mix-up with the update() method is trying to assign its output to a new variable. This won't work as you might expect.
- Because
update()modifies a set in-place, it returnsNone. - Assigning this result to a variable will leave you with an empty object, which often leads to an
AttributeErrorwhen you try to use it later.
Handling unhashable types in sets
Sets require their elements to be "hashable," meaning they must be immutable objects whose value never changes. This is why you'll get a TypeError: unhashable type: 'list' if you try to add a list as an element to a set.
- The fix is to convert mutable items into their immutable counterparts before adding them. For instance, you can change a list into a tuple, which is hashable and can be included in a set.
Fixing TypeError when using the | operator with non-set objects
The pipe operator | is exclusively for set operations. Attempting to use it on other iterables, such as lists, will immediately trigger a TypeError because the operation isn't supported for those data types. The following code snippet shows this common error in action.
items1 = [1, 2, 3]
items2 = [3, 4, 5]
merged = items1 | items2 # TypeError: unsupported operand type(s) for |: 'list' and 'list'
print(merged)
The code triggers a TypeError because the | operator is reserved for sets, not lists like items1 and items2. To merge them correctly, you need a different approach. The following snippet shows how it's done.
items1 = [1, 2, 3]
items2 = [3, 4, 5]
merged = set(items1) | set(items2)
print(merged) # {1, 2, 3, 4, 5}
The solution is to explicitly convert each list into a set before the union. By wrapping each list in the set() constructor, you create new set objects that are compatible with the | operator. This is a common scenario when you're combining data from different sources, like a list from an API and a local set.
- As an alternative, you can use the
union()method, which handles different iterable types automatically without needing prior conversion.
Avoiding common mistakes with the update() method
It’s a common mistake to treat the update() method like you would union(), expecting it to return a new set. Because update() modifies a set in-place, its actual return value is None. This can lead to bugs when you assign the result to a variable, as the code below demonstrates.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.update(set2) # update() returns None
print(result) # Prints None
The code attempts to capture the merged set in the result variable. But since update() modifies set1 directly instead of returning a new set, result is assigned a None value. See the correct implementation below.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2) # Modifies set1 in-place
print(set1) # {1, 2, 3, 4, 5}
To correctly use the update() method, call it directly on the set you intend to modify. The operation changes set1 in-place, absorbing the elements from set2. After the call, simply use set1, which now contains the complete, merged collection.
- This in-place modification is memory-efficient, making it ideal when you're working with large datasets and want to avoid creating new objects.
Handling unhashable types in sets
A set's elements must be immutable, meaning they can't change. This rule is why you'll encounter a TypeError if you try to include a mutable object, like a list, inside a set. The following code demonstrates this common error.
user_tags = {"python", "beginner"}
user_skills = {"python", ["html", "css"]} # TypeError: unhashable type: 'list'
all_user_info = user_tags | user_skills
print(all_user_info)
The code fails because the user_skills set contains a list, ["html", "css"]. Since lists are mutable, Python cannot add them to a set. The following snippet shows how to correctly handle this by using an immutable type.
user_tags = {"python", "beginner"}
user_skills = {"python", ("html", "css")} # Using tuple instead of list
all_user_info = user_tags | user_skills
print(all_user_info) # {'python', 'beginner', ('html', 'css')}
The fix is to replace the mutable list with an immutable tuple. By changing the list to ("html", "css"), the set now contains only hashable elements, allowing the union operation to succeed. This is a simple but crucial adjustment.
- You'll often encounter this when working with nested collections, so always ensure any item added to a set is immutable, like a tuple instead of a list.
Real-world applications
Now that you're familiar with the methods and how to troubleshoot them, you can apply these techniques to solve real-world problems.
Creating personalized recommendations with the | operator
You can create a comprehensive user profile by merging different sets of interests, like browsing history and purchase data, using the | operator.
user_preferences_from_browsing = {"science", "technology", "programming"}
user_preferences_from_purchases = {"books", "programming", "gadgets"}
all_interests = user_preferences_from_browsing | user_preferences_from_purchases
print(f"Recommended categories: {all_interests}")
This example shows how you can easily combine two different sources of user data. The pipe operator | merges the user_preferences_from_browsing and user_preferences_from_purchases sets, creating a new set called all_interests.
- It’s an efficient way to gather all unique items from both collections into one place.
- The shared interest,
"programming", is automatically deduplicated and appears only once in the final result.
This approach gives you a clean, unified list of interests, perfect for powering features like a personalized recommendation engine.
Analyzing text documents with set operations
You can also use sets to analyze text, making it easy to find common keywords or measure the total vocabulary across different documents.
document1 = "Python is a powerful programming language for data analysis"
document2 = "Data science uses Python for machine learning applications"
doc1_words = set(document1.lower().split())
doc2_words = set(document2.lower().split())
all_keywords = doc1_words | doc2_words
common_keywords = doc1_words & doc2_words
print(f"Common keywords: {common_keywords}")
print(f"Total unique keywords: {len(all_keywords)}")
This code shows how you can use sets for simple text analysis. It starts by converting two strings into sets of lowercase words using lower() and split(), which standardizes the text for accurate comparison.
- The union operator
|gathers every unique word from both documents into a single vocabulary set. - The intersection operator
&identifies only the words that are common to both documents.
The final output gives you the shared keywords and the total size of the combined vocabulary, calculated with len().
Get started with Replit
Put your knowledge into practice and build a real tool. Describe what you want to build, like “a script that merges two contact lists and removes duplicates” or “an app that combines product tags from different sources.”
Replit Agent handles the implementation, from writing the code to testing and deployment. Start building with Replit.
Create and deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.
Create & deploy websites, automations, internal tools, data pipelines and more in any programming language without setup, downloads or extra tools. All in a single cloud workspace with AI built in.


.png)
.png)