Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. set.union() / set.intersection() / set.difference()

set.union() / set.intersection() / set.difference()

Methods and operators for set operations (union, intersection, difference, and symmetric difference). You can perform the same operations as mathematical set theory concisely in Python.

Syntax

# Union (elements in either set)
set1.union(set2)
set1 | set2

# Intersection (elements in both sets)
set1.intersection(set2)
set1 & set2

# Difference (elements in set1 but not in set2)
set1.difference(set2)
set1 - set2

# Symmetric difference (elements in one set but not both)
set1.symmetric_difference(set2)
set1 ^ set2

Methods and Operators

Method / OperatorDescription
set1.union(set2) / |Returns a new set containing all elements from set1 and set2 (union).
set1.intersection(set2) / &Returns a new set containing only the elements found in both set1 and set2 (intersection).
set1.difference(set2) / -Returns a new set with elements from set1 that are not in set2 (difference).
set1.symmetric_difference(set2) / ^Returns a new set containing elements that are in one set but not both (symmetric difference).
set1.intersection_update(set2) / &=Updates set1 in place with the intersection of set1 and set2.
set1.difference_update(set2) / -=Removes elements from set1 that are also in set2, in place.

Sample Code

# Prepare two sets.
python_users = {'Alice', 'Bob', 'Charlie', 'Diana'}
java_users   = {'Bob', 'Eve', 'Diana', 'Frank'}

# Union: users who use either language
print(python_users | java_users)
# Outputs: {'Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'}

# Intersection: users who use both languages
print(python_users & java_users)
# Outputs: {'Bob', 'Diana'}

# Difference: users who use only Python
print(python_users - java_users)
# Outputs: {'Alice', 'Charlie'}

# Symmetric difference: users who use only one language
print(python_users ^ java_users)
# Outputs: {'Alice', 'Charlie', 'Eve', 'Frank'}

# You can also use methods for the same operations.
union_set = python_users.union(java_users)
inter_set = python_users.intersection(java_users)
diff_set  = python_users.difference(java_users)

# In-place operators update the set itself.
python_users &= java_users
print(python_users)  # Updated to the intersection result.

# You can chain operations across multiple sets.
a = {1, 2, 3}
b = {2, 3, 4}
c = {3, 4, 5}
print(a & b & c)  # Outputs: {3}
print(a | b | c)  # Outputs: {1, 2, 3, 4, 5}

Notes

Set operations can be performed with either methods or operators. When using operators (|, &, -, ^), both operands must be sets. When using methods, any iterable such as a list or tuple can be passed as the argument.

All set operations return a new set without modifying the originals. To update a set in place, use the in-place operators |=, &=, -=, and ^=, or their corresponding methods (update(), intersection_update(), etc.).

The difference operation is order-dependent. A - B and B - A produce different results. If you want elements that belong to neither set, use the symmetric difference (^) instead.

To check subset or superset relationships between sets, see 'set.issubset() / set.issuperset() / set.isdisjoint()'.

If you find any errors or copyright issues, please .