Python Set Methods
Python sets are unordered collections of unique elements, meaning they do not allow duplicates. Python provides a variety of set methods that help perform operations like adding, removing, and manipulating sets. Here’s an overview of the most commonly used set methods:
1. Adding Elements to a Set
add(item): Adds a single item to the set. If the item already exists, it does nothing.my_set = {1, 2, 3} my_set.add(4) # Output: {1, 2, 3, 4}update(iterable): Adds multiple elements from an iterable (like a list, tuple, or another set) to the set.my_set = {1, 2, 3} my_set.update([4, 5]) # Output: {1, 2, 3, 4, 5}
2. Removing Elements from a Set
remove(item): Removes the specified element from the set. Raises aKeyErrorif the item is not found.my_set = {1, 2, 3} my_set.remove(2) # Output: {1, 3}discard(item): Removes the specified element from the set if it exists. Does nothing if the item is not found (no error is raised).my_set = {1, 2, 3} my_set.discard(2) # Output: {1, 3} my_set.discard(4) # No errorpop(): Removes and returns an arbitrary element from the set. Raises aKeyErrorif the set is empty.my_set = {1, 2, 3} my_set.pop() # Output: 1 (or any element, since set is unordered)clear(): Removes all elements from the set, leaving it empty.my_set = {1, 2, 3} my_set.clear() # Output: set()
3. Set Operations
Python sets support standard set operations, which include union, intersection, difference, and symmetric difference:
union(*other_sets)or|: Returns a new set containing all elements from the set and the other sets.set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.union(set2) # Output: {1, 2, 3, 4, 5}intersection(*other_sets)or&: Returns a new set containing only the common elements from the set and the other sets.set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.intersection(set2) # Output: {3}difference(*other_sets)or-: Returns a new set containing elements that are in the set but not in the other sets.set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.difference(set2) # Output: {1, 2}symmetric_difference(other_set)or^: Returns a new set containing elements that are in either of the sets but not in both.set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1.symmetric_difference(set2) # Output: {1, 2, 4, 5}
4. Subset and Superset Methods
issubset(other_set): ReturnsTrueif all elements of the set are in theother_set.set1 = {1, 2} set2 = {1, 2, 3, 4} result = set1.issubset(set2) # Output: Trueissuperset(other_set): ReturnsTrueif the set contains all elements of theother_set.set1 = {1, 2, 3, 4} set2 = {1, 2} result = set1.issuperset(set2) # Output: Trueisdisjoint(other_set): ReturnsTrueif the set has no elements in common with theother_set.set1 = {1, 2, 3} set2 = {4, 5, 6} result = set1.isdisjoint(set2) # Output: True
5. Copying a Set
copy(): Returns a shallow copy of the set.my_set = {1, 2, 3} new_set = my_set.copy() # Output: {1, 2, 3}
6. Length and Membership
len(set): Returns the number of elements in the set.my_set = {1, 2, 3} print(len(my_set)) # Output: 3inoperator: Checks if an element is in the set.my_set = {1, 2, 3} print(2 in my_set) # Output: True
Example:
Here’s an example combining several set methods:
# Creating sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Adding an element
set1.add(6) # {1, 2, 3, 6}
# Removing an element
set1.remove(2) # {1, 3, 6}
# Set operations
union_set = set1.union(set2) # {1, 3, 4, 5, 6}
intersection_set = set1.intersection(set2) # {3}
difference_set = set1.difference(set2) # {1, 6}
# Checking subset/superset
is_subset = {1, 3}.issubset(set1) # True
is_superset = set1.issuperset({1, 3}) # True
Summary:
- Adding elements:
add(),update() - Removing elements:
remove(),discard(),pop(),clear() - Set operations:
union(),intersection(),difference(),symmetric_difference() - Subset and superset:
issubset(),issuperset(),isdisjoint() - Copying a set:
copy() - Other operations:
len(), membership within
Sets are useful when you need unique elements and fast membership checks, and Python provides various methods to manipulate them efficiently.