Python sets are unordered collections of unique elements. Sets are mutable, meaning you can add or remove elements after a set is created. However, the elements themselves must be immutable.
A set in Python is created by placing all the elements inside curly braces {}
, separated by commas, or by using the set()
function.
Example:
my_set = {1, 2, 3}
empty_set = set() # Use set() to create an empty set
Sets automatically remove duplicate elements.
my_set = {1, 2, 2, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}
add(item)
: Add an element to the set.
my_set = {1, 2, 3}
my_set.add(4) # {1, 2, 3, 4}
remove(item)
: Remove an element from the set. Raises a KeyError
if the item is not found.
my_set.remove(3) # {1, 2, 4}
discard(item)
: Remove an element from the set if it is present. Does nothing if the item is not found.
my_set.discard(5) # No error even though 5 is not in the set
pop()
: Remove and return an arbitrary element from the set. Raises a KeyError
if the set is empty.
item = my_set.pop()
clear()
: Remove all elements from the set.
my_set.clear() # {}
Union (|
): Combine two sets to include all elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # {1, 2, 3, 4, 5}
Intersection (&
): Get elements that are common to both sets.
intersection_set = set1 & set2 # {3}
Difference (-
): Get elements that are in the first set but not in the second set.
difference_set = set1 - set2 # {1, 2}
Symmetric Difference (^
): Get elements that are in either of the sets but not in both.
sym_diff_set = set1 ^ set2 # {1, 2, 4, 5}
exists = 2 in set1 # True
copy()
: Return a shallow copy of the set.
copy_set = my_set.copy()
update(other_set)
: Update the set with the union of itself and another set.
set1.update(set2) # set1 becomes {1, 2, 3, 4, 5}
intersection_update(other_set)
: Update the set with the intersection of itself and another set.
set1.intersection_update(set2) # set1 becomes {3}
difference_update(other_set)
: Update the set with the difference of itself and another set.
set1.difference_update(set2) # set1 becomes {1, 2}
symmetric_difference_update(other_set)
: Update the set with the symmetric difference of itself and another set.
set1.symmetric_difference_update(set2) # set1 becomes {1, 2, 4, 5}
issubset(other_set)
: Check if the set is a subset of another set.
set1 = {1, 2}
set2 = {1, 2, 3}
is_subset = set1.issubset(set2) # True
issuperset(other_set)
: Check if the set is a superset of another set.
is_superset = set2.issuperset(set1) # True
isdisjoint(other_set)
: Check if two sets have no elements in common.
set3 = {4, 5}
is_disjoint = set1.isdisjoint(set3) # True
Mutable Elements: Sets cannot contain mutable elements like lists or dictionaries because they are unhashable.
invalid_set = {[1, 2], 3} # Raises TypeError
Duplicate Elements: Adding duplicate elements to a set will silently discard the duplicates.
my_set = {1, 2, 2, 3}
print(my_set) # Output: {1, 2, 3}
Pop on Empty Set: Calling pop()
on an empty set raises a KeyError
.
empty_set = set()
empty_set.pop() # Raises KeyError
Frozensets: Immutable versions of sets. Useful when you need a set that cannot be modified after creation.
my_frozenset = frozenset([1, 2, 3])
Lists: If you need an ordered collection or allow duplicates, use a list.
Dictionaries: If you need to store key-value pairs, use a dictionary.
This comprehensive guide should help you understand Python sets, their features, and best practices for using them effectively in your programs.