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.

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.

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)

Output:

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 data and data2. We then use the | operator to perform the union operation on the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

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)

Output:

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 data and data2. We then use the & operator to perform the intersection operation on the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

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)

Output:

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

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

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)

Output:

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 data and data2. We then use the ^ operator to perform the symmetric difference operation on the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

The subset operation returns True 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)

Output:

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

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

The superset operation returns True 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)

Output:

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

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

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.

In Python, we can use the for loop to iterate over sets. In the for loop, we can use the in 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)

Output:

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 data. We then use the for loop to iterate over the set and print the items.

In Python, we can use the while loop to iterate over sets. In the while loop, we can use the in 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

Output:

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 data. We then use the while loop to iterate over the set and print the items.

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)

Output:

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 data. We then use the for loop to iterate over the range of numbers from 1 to 10 and add the numbers to the set using the add() method. The output shows that the items are added to the set.

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

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)

Output:

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 data and data2. We then use the | operator to join the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

In Python, we can use the 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)

Output:

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 data and data2. We then use the update() method to join the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

In Python, we can use the 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)

Output:

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 data and data2. We then use the union() method to join the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

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)

Output:

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 data and data2. We then use the * operator to unpack the two sets and assign the result to the variable data3. The output shows that the items are added to the set.

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

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

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'}

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

In Python, we can use the in keyword to check if an item exists in a set. In this section, we are going to learn how to use the in 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')

Output:

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 data. We then use the in keyword to check if the item a exists in the set. The output shows that the item a exists in the set.

sketch Membership in a list costs more the bigger it gets; in a set it does not p5.js
Searching a list means walking it, so the cost grows with the list. A set hashes the value and looks in one place, so the cost barely moves. These are measured microseconds per lookup for a value that is absent -- the worst case, and the one that matters, because a present value can stop early. At a hundred thousand items the list is about twenty-three thousand times slower.

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

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

Output:

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

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

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.


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading