Skip to content

Add and Remove Items from Set

Python Set Operations

Python provides different methods and operators to perform set operations. In this tutorial, we will learn how to perform set operations in Python.

Add Items to Set

Adding items to the set is very easy. We can use items using two methods. We can use the add()add() method to add a single item to the set. We can use the update()update() method to add multiple items to the set.

add() Method

We can use the add()add() method to add a single item to the set. The add()add() method takes a single argument and adds it to the set. The add()add() method does not return any value. The add()add() method does not add an item to the set if it is already present in the set.

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

Output:

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

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

update() Method

We can use the update()update() method to add multiple items to the set. The update()update() method takes a single argument and adds it to the set. The update()update() method does not return any value. The update()update() method does not add an item to the set if it is already present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then add the items of the data2data2 set to the datadata set using the update()update() method. The output shows that the items are added to the set.

Add Items from Another Data Type

Another way to add multiple items from another data type to the set is to use the update()update() method with another data type. We can pass a list, tuple, dictionary, or another set to the update()update() method to add multiple items to the set.

Add Items from List

We can pass a list to the update()update() method to add multiple items from the list to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a list and assign it to the variable data2data2. We then add the items of the data2data2 list to the datadata set using the update()update() method. The output shows that the items are added to the set.

Add Items from Tuple

We can pass a tuple to the update()update() method to add multiple items from the tuple to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a tuple and assign it to the variable data2data2. We then add the items of the data2data2 tuple to the datadata set using the update()update() method. The output shows that the items are added to the set.

Add Items from String

We can pass a string to the update()update() method to add multiple items from the string to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a string and assign it to the variable data2data2. We then add the items of the data2data2 string to the datadata set using the update()update() method. The output shows that the items are added to the set.

union() Method

We can use the union()union() method to add multiple items to the set. The union()union() method takes a single argument and adds it to the set. The union()union() method does not return any value. The union()union() method does not add an item to the set if it is already present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then add the items of the data2data2 set to the datadata set using the union()union() method. The output shows that the items are added to the set. The union()union() method does not change the original set.

union() Method with Another Data Type

Another way to add multiple items from another data type to the set is to use the union()union() method with another data type. We can pass a list, tuple, dictionary, or another set to the union()union() method to add multiple items to the set.

union() Method with List

We can pass a list to the union()union() method to add multiple items from the list to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a list and assign it to the variable data2data2. We then add the items of the data2data2 list to the datadata set using the union()union() method. The output shows that the items are added to the set. The union()union() method does not change the original set.

union() Method with Tuple

We can pass a tuple to the union()union() method to add multiple items from the tuple to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a tuple and assign it to the variable data2data2. We then add the items of the data2data2 tuple to the datadata set using the union()union() method. The output shows that the items are added to the set. The union()union() method does not change the original set.

union() Method with String

We can pass a string to the union()union() method to add multiple items from the string to the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then declare a string and assign it to the variable data2data2. We then add the items of the data2data2 string to the datadata set using the union()union() method. The output shows that the items are added to the set. The union()union() method does not change the original set.

| Operator

We can use the || operator to add multiple items to the set. The || operator takes a single argument and adds it to the set. The || operator does not return any value. The || operator does not add an item to the set if it is already present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then add the items of the data2data2 set to the datadata set using the || operator. The output shows that the items are added to the set. The || operator does not change the original set.

Remove Items from Set

Removing items from the set is very easy. We can use items using two methods. We can use the remove()remove() method to remove a single item from the set. We can use the discard()discard() method to remove multiple items from the set.

remove() Method

We can use the remove()remove() method to remove a single item from the set. The remove()remove() method takes a single argument and removes it from the set. The remove()remove() method does not return any value. The remove()remove() method raises an error if the item is not present in the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then remove an item from the set using the remove()remove() method. The output shows that the item is removed from the set.

discard() Method

We can use the discard()discard() method to remove multiple items from the set. The discard()discard() method takes a single argument and removes it from the set. The discard()discard() method does not return any value. The discard()discard() method does not raise an error if the item is not present in the set.

set_discard.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('f')
print(data)
data.discard('h')
print(data)
set_discard.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('f')
print(data)
data.discard('h')
print(data)

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then remove an item from the set using the discard()discard() method. The output shows that the item is removed from the set. We then remove an item from the set using the discard()discard() method. The output shows that the item is not removed from the set.

pop() Method

We can use the pop()pop() method to remove an item from the set. The pop()pop() method does not take any argument and removes an item from the set. The pop()pop() method returns the removed item. The pop()pop() method removes the last item from the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then remove an item from the set using the pop()pop() method. The output shows that the item is removed from the set. The pop()pop() method removes the last item from the set.

clear() Method

We can use the clear()clear() method to remove all items from the set. The clear()clear() method does not take any argument and removes all items from the set. The clear()clear() method does not return any value.

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

Output:

command
C:\Users\username>python set_clear.py
set()
command
C:\Users\username>python set_clear.py
set()

In this example, we declare a set and assign it to the variable datadata. We then remove all items from the set using the clear()clear() method. The output shows that the items are removed from the set.

del Keyword

We can use the deldel keyword to remove the set. The deldel keyword does not take any argument and removes the set. The deldel keyword does not return any value.

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

Output:

command
C:\Users\username>python set_del.py
Traceback (most recent call last):
  File "set_del.py", line 3, in <module>
    print(data)
NameError: name 'data' is not defined
command
C:\Users\username>python set_del.py
Traceback (most recent call last):
  File "set_del.py", line 3, in <module>
    print(data)
NameError: name 'data' is not defined

In this example, we declare a set and assign it to the variable datadata. We then remove the set using the deldel keyword. The output shows that the set is removed. The deldel keyword does not return any value.

difference_update() Method

We can use the difference_update()difference_update() method to remove multiple items from the set. The difference_update()difference_update() method takes a single argument and removes it from the set. The difference_update()difference_update() method does not return any value. The difference_update()difference_update() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the difference_update()difference_update() method. The output shows that the items are removed from the set. The difference_update()difference_update() method does change the original set.

difference() Method

We can use the difference()difference() method to remove multiple items from the set. The difference()difference() method takes a single argument and removes it from the set. The difference()difference() method does not return any value. The difference()difference() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the difference()difference() method. The output shows that the items are removed from the set. The difference()difference() method does not change the original set.

intersection_update() Method

We can use the intersection_update()intersection_update() method to remove multiple items from the set. The intersection_update()intersection_update() method takes a single argument and removes it from the set. The intersection_update()intersection_update() method does not return any value. The intersection_update()intersection_update() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the intersection_update()intersection_update() method. The output shows that the items are removed from the set. The intersection_update()intersection_update() method does change the original set.

intersection() Method

We can use the intersection()intersection() method to remove multiple items from the set. The intersection()intersection() method takes a single argument and removes it from the set. The intersection()intersection() method does not return any value. The intersection()intersection() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the intersection()intersection() method. The output shows that the items are removed from the set. The intersection()intersection() method does not change the original set.

symmetric_difference_update() Method

We can use the symmetric_difference_update()symmetric_difference_update() method to remove multiple items from the set. The symmetric_difference_update()symmetric_difference_update() method takes a single argument and removes it from the set. The symmetric_difference_update()symmetric_difference_update() method does not return any value. The symmetric_difference_update()symmetric_difference_update() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the symmetric_difference_update()symmetric_difference_update() method. The output shows that the items are removed from the set. The symmetric_difference_update()symmetric_difference_update() method does change the original set.

symmetric_difference() Method

We can use the symmetric_difference()symmetric_difference() method to remove multiple items from the set. The symmetric_difference()symmetric_difference() method takes a single argument and removes it from the set. The symmetric_difference()symmetric_difference() method does not return any value. The symmetric_difference()symmetric_difference() method does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items of the data2data2 set from the datadata set using the symmetric_difference()symmetric_difference() method. The output shows that the items are removed from the set. The symmetric_difference()symmetric_difference() method does not change the original set.

& Operator

We can use the && operator to remove multiple items from the set. The && operator takes a single argument and removes it from the set. The && operator does not return any value. The && operator does not raise an error if the item is not present in the set.

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

Output:

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

In this example, we declare a set and assign it to the variable datadata. We then remove the items of the data2data2 set from the datadata set using the && operator. The output shows that the items are removed from the set. The && operator does not change the original set.

Conclusion

In this tutorial, we have learned about the Python set. We have learned how to create a set. We have learned how to add items to the set. We have learned how to remove items from the set. We have learned how to use different methods to add and remove items from the set. We have learned how to use different operators to add and remove items from the set. For more tutorials on Python, please visit our Python Central Hub.

Was this page helpful?

Let us know how we did