Sets are an essential data structure in Python, particularly useful for storing unique elements and performing mathematical operations like unions, intersections, and differences. Python provides a wide range of built-in functions and methods to work with sets efficiently. In this tutorial, we’ll cover all the built-in set functions in Python, providing explanations and examples for each.
In Python, a set is an unordered collection of unique elements. Sets are defined using curly braces {} or the set() function, and they allow you to perform mathematical set operations like union, intersection, and difference. Sets do not support indexing, so elements cannot be accessed by index.
Sets are useful in Python for several reasons:
Here’s a complete list of Python’s built-in set functions and methods.
add()
: Adds an element to the set.update()
: Adds multiple elements to the set.remove()
: Removes a specified element from the set.discard()
: Removes a specified element without raising an error if it doesn’t exist.pop()
: Removes and returns a random element from the set.clear()
: Removes all elements from the set.union()
: Returns a union of two sets.intersection()
: Returns the intersection of two sets.difference()
: Returns the difference of two sets.symmetric_difference()
: Returns elements that are in either set but not both.intersection_update()
: Updates a set with the intersection of itself and another.difference_update()
: Updates a set with the difference between itself and another.symmetric_difference_update()
: Updates a set with elements in either set but not both.issubset()
: Checks if the set is a subset of another set.issuperset()
: Checks if the set is a superset of another set.isdisjoint()
: Checks if the set has no elements in common with another set.copy()
: Returns a shallow copy of the set.len()
: Returns the number of elements in the set (not strictly a set method but useful).in
: Checks if an element is in the set.add(element)
: Adds a single element to the set. If the element is already in the set, it has no effect.
fruits = {"apple", "banana"}
fruits.add("cherry")
print(fruits) # Output: {"apple", "banana", "cherry"}
update(iterable)
: Adds multiple elements from an iterable (like a list or tuple) to the set.
fruits = {"apple", "banana"}
fruits.update(["cherry", "orange"])
print(fruits) # Output: {"apple", "banana", "cherry", "orange"}
remove(element)
: Removes a specified element from the set. Raises a KeyError if the element is not found.
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits) # Output: {"apple", "cherry"}
discard(element)
: Removes a specified element without raising an error if the element does not exist.
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits) # Output: {"apple", "cherry"}
fruits.discard("pear") # No error
pop()
: Removes and returns an arbitrary element from the set. Useful for iterating over sets.
fruits = {"apple", "banana", "cherry"}
fruit = fruits.pop()
print(fruit) # Output: One of the elements (since sets are unordered)
print(fruits) # Output: Remaining elements
clear()
: Removes all elements from the set.
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits) # Output: set()
union(set2)
: Returns a new set with all elements from both sets.
set1 = {"apple", "banana"}
set2 = {"cherry", "banana"}
print(set1.union(set2)) # Output: {"apple", "banana", "cherry"}
intersection(set2)
: Returns a new set with only elements that are in both sets.
set1 = {"apple", "banana"}
set2 = {"banana", "cherry"}
print(set1.intersection(set2)) # Output: {"banana"}
difference(set2)
: Returns a new set with elements in the set that are not in the other set.
set1 = {"apple", "banana"}
set2 = {"banana", "cherry"}
print(set1.difference(set2)) # Output: {"apple"}
symmetric_difference(set2)
: Returns a new set with elements in either set but not both.
set1 = {"apple", "banana"}
set2 = {"banana", "cherry"}
print(set1.symmetric_difference(set2)) # Output: {"apple", "cherry"}
intersection_update(set2)
: Updates the set, keeping only elements found in both sets.
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "cherry"}
set1.intersection_update(set2)
print(set1) # Output: {"banana", "cherry"}
difference_update(set2)
: Updates the set, removing elements found in the other set.
set1 = {"apple", "banana", "cherry"}
set2 = {"banana"}
set1.difference_update(set2)
print(set1) # Output: {"apple", "cherry"}
symmetric_difference_update(set2)
: Updates the set with elements found in either set but not both.
set1 = {"apple", "banana"}
set2 = {"banana", "cherry"}
set1.symmetric_difference_update(set2)
print(set1) # Output: {"apple", "cherry"}
issubset(set2)
: Checks if the set is a subset of another set.
set1 = {"apple", "banana"}
set2 = {"apple", "banana", "cherry"}
print(set1.issubset(set2)) # Output: True
issuperset(set2)
: Checks if the set is a superset of another set.
set1 = {"apple", "banana", "cherry"}
set2 = {"banana"}
print(set1.issuperset(set2)) # Output: True
isdisjoint(set2)
: Checks if the set has no elements in common with another set.
set1 = {"apple", "banana"}
set2 = {"cherry", "orange"}
print(set1.isdisjoint(set2)) # Output: True
copy()
: Returns a shallow copy of the set.
fruits = {"apple", "banana", "cherry"}
fruits_copy = fruits.copy()
print(fruits_copy) # Output: {"apple", "banana", "cherry"}
len(set)
: Returns the number of elements in the set.
fruits = {"apple", "banana", "cherry"}
print(len(fruits)) # Output: 3
in
: Checks if an element is in the set.
fruits = {"apple", "banana", "cherry"}
print("banana" in fruits) # Output: True
add()
for a single element and update()
for multiple elements.remove()
to delete specific elements and discard()
if you don’t want errors for non-existent items.union()
, intersection()
, difference()
, and symmetric_difference()
to perform set theory operations.issubset()
, issuperset()
, and isdisjoint()
are valuable for checking relationships between sets.copy()
to clone a set and clear()
to empty it.Python’s built-in set functions provide a powerful way to manage collections of unique elements and perform set theory operations. With functions for adding, removing, testing membership, and manipulating sets, you can handle complex data relationships efficiently. By mastering these functions, you’ll be equipped to work with sets effectively, whether you're handling unique data entries or performing mathematical operations.
With Python’s set functions, you can:
Ready to master sets in Python? Practice these functions with real data to see the power and flexibility of sets in action. Happy coding!