Skip to main content

Join Sets

Python gives you several ways to combine two or more sets.

union() and update() bring together every item from both sets.

intersection() holds on to the duplicates and nothing else.

difference() gives you what the first set has and the other set(s) lack.

symmetric_difference() gives you everything except the duplicates.


Union

Calling union() gives you a brand-new set containing every item of both sets.

Example

Combine set1 and set2 into a third set:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = set1.union(set2) print(set3)
Output: complete Python program

Example

The | operator does the same job for two sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = set1 | set2 print(set3)
Output: complete Python program

Join Multiple Sets

Every joining method and operator works with more than two sets.

With a method, list the extra sets between the parentheses and put commas between them:

Example

Pass three sets to union() to combine four in total:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = {"John", "Elena"} set4 = {"apple", "bananas", "cherry"} myset = set1.union(set2, set3, set4) print(myset)
Output: complete Python program

Example

Chain | to combine four sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = {"John", "Elena"} set4 = {"apple", "bananas", "cherry"} myset = set1 | set2 | set3 |set4 print(myset)
Output: complete Python program

Join a Set and a Tuple

union() also accepts other data types, lists and tuples among them, not just sets.

Whatever you pass in, you get a set back.

Example

Combine a set with a tuple:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. x = {"a", "b", "c"} y = (1, 2, 3) z = x.union(y) print(z)
Output: complete Python program

Update

With update(), the items of one set are poured into another.

It modifies that original set directly instead of giving you a new one.

Example

Here set1 receives every item of set2 through update():

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"a", "b" , "c"} set2 = {1, 2, 3} set1.update(set2) print(set1)
Output: complete Python program

Intersection

Keep the duplicates and nothing else

Calling intersection() creates a new set holding just the items that appear in both sets.

Example

Combine set1 and set2, holding on only to what they share:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.intersection(set2) print(set3)
Output: complete Python program

Example

The & operator gives the same result for two sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1 & set2 print(set3)
Output: complete Python program

Example

With intersection_update(), set1 itself ends up with only what it shares with set2:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set1.intersection_update(set2) print(set1)
Output: complete Python program

Example

Combine sets containing True, False, 1 and 0, then look at which values count as duplicates:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", 1, "banana", 0, "cherry"} set2 = {False, "google", 1, "apple", 2, True} set3 = set1.intersection(set2) print(set3)
Output: complete Python program

Difference

Calling difference() creates a new set with just those items of the first set that the other set does not have.

Example

Get what set1 has and set2 lacks:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.difference(set2) print(set3)
Output: complete Python program

Example

The - operator gives the same result for two sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1 - set2 print(set3)
Output: complete Python program

Example

Call difference_update() to strip from the first set anything the other set also has:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set1.difference_update(set2) print(set1)
Output: complete Python program

Symmetric Differences

Calling symmetric_difference() leaves you with only the elements that belong to one set but not both.

Example

Get the items that appear in just one of the two sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.symmetric_difference(set2) print(set3)
Output: complete Python program

Example

The ^ operator gives the same result for two sets:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1 ^ set2 print(set3)
Output: complete Python program

Example

Call symmetric_difference_update() so that set1 holds only the items found in exactly one set:

Prompt
Write a Python program that demonstrates the code shown in the PromptCookbook example. set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set1.symmetric_difference_update(set2) print(set1)
Output: complete Python program