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
Section titled “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
Section titled “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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data | data2
print(data3)Output:
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.
Intersection (&) Operation
Section titled “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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data & data2
print(data3)Output:
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.
Difference (-) Operation
Section titled “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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data - data2
print(data3)Output:
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.
Symmetric Difference (^) Operation
Section titled “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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data ^ data2
print(data3)Output:
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.
Subset (<=) Operation
Section titled “Subset (<=) Operation”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data <= data2
print(data3)Output:
C:\Users\username>python set_subset.py
FalseIn 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.
Superset (>=) Operation
Section titled “Superset (>=) Operation”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data >= data2
print(data3)Output:
C:\Users\username>python set_superset.py
FalseIn 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.
Set Loops
Section titled “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
Section titled “For Loop”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
for item in data:
print(item)Output:
C:\Users\username>python set_for_loop.py
a
b
d
f
g
c
eIn 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.
While Loop
Section titled “While Loop”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
i = 0
while i < len(data):
print(data[i])
i += 1Output:
C:\Users\username>python set_while_loop.py
a
b
d
f
g
c
eIn 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.
Adding Elements to a Set Using Loops
Section titled “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.
data = set()
for i in range(1, 11):
data.add(i)
print(data)Output:
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.
Set Join
Section titled “Set Join”Joining two sets is called a union operation. In Python, we can use the |, update(), and union() methods to join two sets.
Using the | Operator
Section titled “Using the | Operator”In Python, we can use the | operator to join two sets.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data | data2
print(data3)Output:
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.
Using the update() Method
Section titled “Using the update() Method”In Python, we can use the update() method to join two sets.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data.update(data2)
print(data)Output:
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.
Using the union() Method
Section titled “Using the union() Method”In Python, we can use the union() method to join two sets.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = data.union(data2)
print(data3)Output:
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.
Unpacking Sets
Section titled “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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'d', 'e', 'f', 'g', 'h', 'i', 'j'}
data3 = {*data, *data2}
print(data3)Output:
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.
Set Copy
Section titled “Set Copy”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = data.copy()
data2.add('h')
print(data)
print(data2)Output:
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.
Set Membership
Section titled “Set Membership”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.
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:
C:\Users\username>python set_membership.py
a exists in the setIn 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.
Set Length
Section titled “Set Length”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.
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(len(data))Output:
C:\Users\username>python set_length.py
7In 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.
Conclusion
Section titled “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.
Try it: Set Operations Exercises
Section titled “Try it: Set Operations Exercises”Exercise 1 – Union and Intersection
Section titled “Exercise 1 – Union and Intersection”Exercise 2 – Difference
Section titled “Exercise 2 – Difference”Exercise 3 – Symmetric Difference
Section titled “Exercise 3 – Symmetric Difference”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading