Skip to content

Set Operations

In this tutorial, we are going to learn how to perform set operations in Python. We will cover the union, intersection, difference, and symmetric difference operations. We will also cover the subset and superset operations. We are also going to talk about loops and how to use them to iterate over sets. how to join sets, and how to check if an item exists in a set and also copy a set.

Set Operations

In the set theory, there are several operations that we can perform on sets. In this section, we are going to learn how to perform these operations in Python.

Union (|) Operation

The union operation returns a new set that contains all the elements that are in either set. In Python, we can use the || operator to perform the union operation.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data | data2
print(data3)
set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data | data2
print(data3)

Output:

command
C:\Users\username>python set_union.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}
command
C:\Users\username>python set_union.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the || operator to perform the union operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Intersection (&) Operation

The intersection operation returns a new set that contains the elements that are common in both sets. In Python, we can use the && operator to perform the intersection operation.

set_intersection.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data & data2
print(data3)
set_intersection.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data & data2
print(data3)

Output:

command
C:\Users\username>python set_intersection.py
{'f', 'g', 'd', 'e'}
command
C:\Users\username>python set_intersection.py
{'f', 'g', 'd', 'e'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the && operator to perform the intersection operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Difference (-) Operation

The difference operation returns a new set that contains the elements that are in the first set but not in the second set. In Python, we can use the -- operator to perform the difference operation.

set_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data - data2
print(data3)
set_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data - data2
print(data3)

Output:

command
C:\Users\username>python set_difference.py
{'a', 'b', 'c'}
command
C:\Users\username>python set_difference.py
{'a', 'b', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the -- operator to perform the difference operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Symmetric Difference (^) Operation

The symmetric difference operation returns a new set that contains the elements that are in either set but not in both sets. In Python, we can use the ^^ operator to perform the symmetric difference operation.

set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data ^ data2
print(data3)
set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data ^ data2
print(data3)

Output:

command
C:\Users\username>python set_symmetric_difference.py
{'j', 'b', 'c', 'a', 'i', 'h'}
command
C:\Users\username>python set_symmetric_difference.py
{'j', 'b', 'c', 'a', 'i', 'h'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the ^^ operator to perform the symmetric difference operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Subset (<=) Operation

The subset operation returns TrueTrue if all the elements of the first set are in the second set. In Python, we can use the <=<= operator to perform the subset operation.

set_subset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data <= data2
print(data3)
set_subset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data <= data2
print(data3)

Output:

command
C:\Users\username>python set_subset.py
False
command
C:\Users\username>python set_subset.py
False

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the <=<= operator to perform the subset operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Superset (>=) Operation

The superset operation returns TrueTrue if all the elements of the second set are in the first set. In Python, we can use the >=>= operator to perform the superset operation.

set_superset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data >= data2
print(data3)
set_superset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data >= data2
print(data3)

Output:

command
C:\Users\username>python set_superset.py
False
command
C:\Users\username>python set_superset.py
False

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the >=>= operator to perform the superset operation on the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Set Loops

In Python, we can use loops to iterate over sets. In this section, we are going to learn how to use loops to iterate over sets.

For Loop

In Python, we can use the forfor loop to iterate over sets. In the forfor loop, we can use the inin keyword to check if an item exists in a set.

set_for_loop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for item in data:
    print(item)
set_for_loop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for item in data:
    print(item)

Output:

command
C:\Users\username>python set_for_loop.py
a
b
d
f
g
c
e
command
C:\Users\username>python set_for_loop.py
a
b
d
f
g
c
e

In this example, we declare a set and assign it to the variable datadata. We then use the forfor loop to iterate over the set and print the items.

While Loop

In Python, we can use the whilewhile loop to iterate over sets. In the whilewhile loop, we can use the inin keyword to check if an item exists in a set.

set_while_loop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
    print(data[i])
    i += 1
set_while_loop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
    print(data[i])
    i += 1

Output:

command
C:\Users\username>python set_while_loop.py
a
b
d
f
g
c
e
command
C:\Users\username>python set_while_loop.py
a
b
d
f
g
c
e

In this example, we declare a set and assign it to the variable datadata. We then use the whilewhile loop to iterate over the set and print the items.

Adding Elements to a Set Using Loops

In Python, we can use loops to add elements to a set. In this section, we are going to learn how to use loops to add elements to a set.

set_add_elements.py
data = set()
for i in range(1, 11):
    data.add(i)
print(data)
set_add_elements.py
data = set()
for i in range(1, 11):
    data.add(i)
print(data)

Output:

command
C:\Users\username>python set_add_elements.py
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
command
C:\Users\username>python set_add_elements.py
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

In this example, we declare an empty set and assign it to the variable datadata. We then use the forfor loop to iterate over the range of numbers from 1 to 10 and add the numbers to the set using the add()add() method. The output shows that the items are added to the set.

Set Join

Joining two sets is called a union operation. In Python, we can use the ||, update()update(), and union()union() methods to join two sets.

Using the || Operator

In Python, we can use the || operator to join two sets.

set_join.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data | data2
print(data3)
set_join.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data | data2
print(data3)

Output:

command
C:\Users\username>python set_join.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}
command
C:\Users\username>python set_join.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the || operator to join the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Using the update()update() Method

In Python, we can use the update()update() method to join two sets.

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.update(data2)
print(data)
set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.update(data2)
print(data)

Output:

command
C:\Users\username>python set_update.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}
command
C:\Users\username>python set_update.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the update()update() method to join the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Using the union()union() Method

In Python, we can use the union()union() method to join two sets.

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data.union(data2)
print(data3)
set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data.union(data2)
print(data3)

Output:

command
C:\Users\username>python set_union.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}
command
C:\Users\username>python set_union.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the union()union() method to join the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Unpacking Sets

In Python, we can use the ** operator to unpack sets. In this section, we are going to learn how to use the ** operator to unpack sets.

set_unpack.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = {*data, *data2}
print(data3)
set_unpack.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = {*data, *data2}
print(data3)

Output:

command
C:\Users\username>python set_unpack.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}
command
C:\Users\username>python set_unpack.py
{'d', 'j', 'e', 'a', 'b', 'f', 'i', 'h', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then use the ** operator to unpack the two sets and assign the result to the variable data3data3. The output shows that the items are added to the set.

Set Copy

In Python, we can use the copy()copy() method to copy a set. In this section, we are going to learn how to use the copy()copy() method to copy a set.

Now, let’s see how to copy a set using the copy()copy() method.

set_copy.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = data.copy()
data2.add('h')
print(data)
print(data2)
set_copy.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = data.copy()
data2.add('h')
print(data)
print(data2)

Output:

command
C:\Users\username>python set_copy.py
{'d', 'e', 'a', 'b', 'f', 'g', 'c'}
{'d', 'e', 'a', 'b', 'f', 'g', 'h', 'c'}
command
C:\Users\username>python set_copy.py
{'d', 'e', 'a', 'b', 'f', 'g', 'c'}
{'d', 'e', 'a', 'b', 'f', 'g', 'h', 'c'}

In this example, we declare a set and assign it to the variable datadata. We then use the copy()copy() method to copy the datadata set to the variable data2data2. We then add the item hh to the data2data2 set. The output shows that the item hh is added to the data2data2 set only.

Set Membership

In Python, we can use the inin keyword to check if an item exists in a set. In this section, we are going to learn how to use the inin keyword to check if an item exists in a set.

set_membership.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'a' in data:
    print('a exists in the set')
else:
    print('a does not exist in the set')
set_membership.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
if 'a' in data:
    print('a exists in the set')
else:
    print('a does not exist in the set')

Output:

command
C:\Users\username>python set_membership.py
a exists in the set
command
C:\Users\username>python set_membership.py
a exists in the set

In this example, we declare a set and assign it to the variable datadata. We then use the inin keyword to check if the item aa exists in the set. The output shows that the item aa exists in the set.

Set Length

In Python, we can use the len()len() method to get the length of a set. In this section, we are going to learn how to use the len()len() method to get the length of a set.

set_length.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(len(data))
set_length.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(len(data))

Output:

command
C:\Users\username>python set_length.py
7
command
C:\Users\username>python set_length.py
7

In this example, we declare a set and assign it to the variable datadata. We then use the len()len() method to get the length of the set. The output shows that the length of the set is 7.

Conclusion

In this tutorial, we learned how to perform set operations in Python. We covered the union, intersection, difference, and symmetric difference operations. We also covered the subset and superset operations. We also talked about loops and how to use them to iterate over sets. We also learned how to join sets, and how to check if an item exists in a set and also copy a set. Now you can use sets in your Python programs. To learn more about sets, visit the Python sets page. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did