Python Set Methods

Spread the love

Python provides several set methods to perform a wide range of operations on set objects. Python Sets are a collection of unique elements, similar to a list or a tuple, but with some key differences. In this article, we will discuss the various Python Set Methods with examples.

1. Python Set Methods

Python set MethodsDescription
add()Add elements to the set object.
clear()Removes all elements from the set.
copy()Copy the set object to another set object.
difference()Returns the difference between two sets as a new set.
difference_update()Updates the difference on the called set.
symmetric_difference()Returns elements that are not present in each other.
symmetric_difference_update()Updates the difference on the called set.
remove()Removes the specified element from the list. Returns error when the element is not present.
discard()Removes the specified element from the list. Does nothing when the element is not present.
pop()Returns an arbitrary element from the set.
intersection()Returns a new set that contains the elements that are common to both sets.
union()Returns a new set by combining elements from both sets.
update()Add a list or set to the set object.
isdisjoint()Returns True if the sets are disjoint, and False if they are not
issubset()Checks if a set is a subset of another set.
issuperset()Checks if s set is a superset of another set.
Python set Methods

2. add() – Add an element to set

The set.add() method is used to add an element to the set object. It takes a single argument, which is the element that you want to add to the set. The set.add() method can only be used to add a single element to a set at a time. The syntax of the method is set.add(element).

Here is an example of using the set.add() method to add an element to a set:


# Create set
languages = {'Python', 'C++', 'Java'}

# Add element to set
languages.add('Go')
print(languages)  

# Output: 
# {'Python', 'C++', 'Java', 'Go'}

3. clear() – Remove elements from a set

Use the set.clear() method to remove all the elements from the Python set. The set.clear() method is permanent and cannot be undone. This method does not take any arguments and does not return any value. When this method is called on a set, it simply removes all the elements from the set.


# Remove all elements from set
languages = {'Python', 'C++', 'Java'}
languages.clear()
print(languages)  

# Output: 
# set()

4. copy() – Create a copy of a set

To create a copy of a set, you can use the set.copy() method. This Python set method does not take any arguments and returns a new set that is a copy of the original set.

  • The set.copy() method is useful when you want to create a new set based on an existing set, but do not want to modify the original set.
  • It is also useful as a way to create a backup of a set before making changes to it, as the copy is independent of the original set.

# Create set
original_set = {'Python', 'C++', 'Java'}

# Create a copy of the set
new_set = original_set.copy()
print(new_set) 
 
# Output: 
# {'Python', 'C++', 'Java'}

# Modify the original set
original_set.add('Go')
print(original_set)  

# Output: 
# {'Python', 'C++', 'Java', 'Go'}

# Print the copy
print(new_set)  

# Output: 
# {'Python', 'C++', 'Java'}

5. union() – Union of Two Sets

The set.union() method returns a new set that contains all the elements from both sets. It does not modify the original sets. The set.union() method returns a new set and does not modify the original sets.

The set.union() method can take multiple sets as arguments, in which case it returns the union of all the sets. See the following example.


# Find the union of three sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}
union = set1.union(set2, set3)
print(union) 
 
# Output: 
# {1, 2, 3, 4, 5, 6, 7}

6. intersection() – Get Intersection of Two Sets

The set.intersection() method returns a new set that contains the elements that are common to both sets. It does not modify the original sets. The set.intersection() method returns a new set and does not modify the original sets. The syntax of the method is new_set = set.intersection(other_set).


# Find the intersection of two sets
set1 = {'Python', 'Java', 'C++'}
set2 = {'Java', 'C++', 'Go'}
intersection = set1.intersection(set2)
print(intersection)
  
# Output: 
# {'Java', 'C++'}

7. update() – Add Multiple Elements to Set

The set.update() method adds the elements of an iterable (such as a list or a set) to the set. It can also take multiple sets as arguments, in which case it adds all the elements of the sets to the set. It modifies the set in place and does not return a new set.


# Add multiple elements to a set
s = {1, 2, 3}
s.update([4, 5, 6])
print(s)  

# Output: 
# {1, 2, 3, 4, 5, 6}

# Add multiple sets to a set
s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = {5, 6, 7}
s1.update(s2, s3)
print(s1)  

# Output: 
# {1, 2, 3, 4, 5, 6, 7}

8. Set Differences Methods in Python

There are several methods to find the difference between two sets in Python. These methods allow you to find the difference between two sets in different ways, depending on your specific needs and requirements.

Here are 4 different ways of finding the difference between the two sets.

8.1 difference() method

This method returns the difference between two sets as a new set. This method does not modify the original sets.


# Create two sets
set1 = {1, 2, 3}
set2 = {2, 3, 4}

# difference()
difference = set1.difference(set2)
print(difference)  

# Output: 
# {1}

8.2 difference_update() method

This method is used to update a set with the elements that are present in the set but not in the specified iterable. . It modifies the original set in place and does not return any value.


# difference_update()
set1.difference_update(set2)
print(set1)  

# Output: 
# {1}

8.3 symmetric_difference() method

This method returns the symmetric difference of two sets as a new set. This method does not modify the original sets. The symmetric difference of two sets is the set of elements that are present in one set but not in the other.


# symmetric_difference()
symmetric_difference = set1.symmetric_difference(set2)
print(symmetric_difference)  

# Output: 
# {1, 4}

8.4 symmetric_difference_update() method

This method is used to update a set with the symmetric difference of itself and another set. It modifies the original set in place and does not return any value.


# symmetric_difference_update()
set1.symmetric_difference_update(set2)
print(set1)  

# Output: 
# {1, 4}

9. Remove Element From a Set

There are 3 different ways to remove elements from a list. Each of these Python set methods uses different approaches to remove the elements.

9.1 remove() method

The remove() method removes the specified element from the Python set. If the element is not present in the set, it raises a KeyError exception.


# Remove an element from a set
s = {'Python', 'C++', 'Java'}
s.remove('C++')
print(s)  

# Output: 
# { 'Python', 'Java'}

# Remove an element that is not present
s.remove('Go')  

# Output: 
# KeyError: 'Go'

9.2 discard() method

This discard() method removes the specified element from the set. If the element is not present in the set, it does nothing.


# Remove an element from a set
s = {'Python', 'C++', 'Java'}
s.discard('C++')
print(s)  

# Output: 
# {'Python', 'Java'}

# Remove element that is not present in the set
s.discard('Go')  

# No output or exception

9.3 pop() method

The pop() method returns an arbitrary element from the set. If the set is empty, it raises an KeyError exception.


# Remove an element from a set
s = {1, 2, 3}
element = s.pop()
print(element) 
 
# Output: 
# 1

print(s)  
# Output: {2, 3}

# Remove an element from an empty set
s = set()
element = s.pop()  

# Output: 
# KeyError: 'pop from an empty set'

10. isdisjoint() – Check Two Sets are Disjoint

The set.isdisjoint() method returns True if the sets are disjoint, and False if they are not. It does not modify the original sets. A set is disjoint if it has no elements in common with another set. The set.isdisjoint() method returns a Boolean value and does not modify the original sets.


# Check if two sets are disjoint
set1 = {1, 2, 3}
set2 = {4, 5, 6}
result = set1.isdisjoint(set2)
print(result)  # Output: True

set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.isdisjoint(set2)
print(result)  # Output: False

11. issubset() – Check Set is Subset of Another

The set.issubset() method returns True if the first set is a subset of the second set, and False if it is not. It does not modify the original sets. A set is a subset of another set if all the elements of the first set are also present in the second set.


# Check if one set is a subset of another
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
result = set1.issubset(set2)
print(result)  

# Output: 
# True

# Another example
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.issubset(set2)
print(result)
  
# Output: 
# False

12. issuperset() – Check Set is Superset of Another

The set.issuperset() method returns True if the first set is a superset of the second set, and False if it is not. It does not modify the original sets. A set is a superset of another set if it contains all the elements of the other set.


# Check if one set is a superset of another
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
result = set1.issuperset(set2)
print(result)  

# Output: 
True

# Another example
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.issuperset(set2)
print(result) 
 
# Output: 
# False

Summary and Conclusion

In this article, you have learned all set methods in Python. You now know everything about the Python set methods, you can use these to add, remove, and manipulate the elements of a set in a variety of ways. If you have any questions, feel free to ask in the comments.

AlixaProDev

I am a software Engineer with extensive 4+ years of experience in Programming related content Creation.

Leave a Reply

You are currently viewing Python Set Methods