Sets in Python
In Python, a
set
is an unordered collection of unique elements. A set is defined using curly braces (
{}
) or the
set()
constructor. Unlike lists and tuples, sets are not indexed, and their elements are not ordered. This makes sets more efficient for certain operations, such as membership testing and removing duplicates.
Creating Sets
To create a set, you can use curly braces and separate the elements with commas, or you can use the
set()
constructor and pass in an iterable:
# Creating a set with curly braces
my_set = {1, 2, 3}
# Creating a set with the set() constructor
my_other_set = set([4, 5, 6])
Note that when using the
set()
constructor with a list or other iterable, duplicates are automatically removed:
# Creating a set from a list with duplicates
my_list = [1, 2, 2, 3, 3, 3]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3}
Accessing Elements
Since sets are not indexed, you cannot access elements by their position. However, you can check if an element is in a set using the
in
operator:
# Creating a set
my_set = {1, 2, 3}
# Checking if an element is in the set
if 2 in my_set:
print("2 is in the set") # Output: "2 is in the set"
Modifying Sets
Unlike tuples and strings, sets are mutable, which means you can add and remove elements from them:
# Creating a set
my_set = {1, 2, 3}
# Adding an element to the set
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
# Removing an element from the set
my_set.remove(3)
print(my_set) # Output: {1, 2, 4}
There are many other methods available for modifying sets, such as
discard()
,
pop()
,
clear()
, and
update()
. Refer to the Python documentation for more information.
Set Operations
Sets support several operations that can be used to combine, compare, and manipulate sets:
- Union: Returns a set containing all the elements from both sets.
- Intersection: Returns a set containing only the elements that are common to both sets.
- Difference: Returns a set containing the elements that are in the firstset but not in the second set.
- Symmetric difference: Returns a set containing the elements that are in either the first or second set, but not both.
-
Subset:
Returns
True
if all the elements of one set are in the other set. -
Superset:
Returns
True
if all the elements of the other set are in this set.
You can perform these operations using operators or methods:
# Creating two sets
set_a = {1, 2, 3}
set_b = {3, 4, 5}
# Union using the '|' operator or the union() method
print(set_a | set_b) # Output: {1, 2, 3, 4, 5}
print(set_a.union(set_b)) # Output: {1, 2, 3, 4, 5}
# Intersection using the '&' operator or the intersection() method
print(set_a & set_b) # Output: {3}
print(set_a.intersection(set_b)) # Output: {3}
# Difference using the '-' operator or the difference() method
print(set_a - set_b) # Output: {1, 2}
print(set_a.difference(set_b)) # Output: {1, 2}
# Symmetric difference using the '^' operator or the symmetric_difference() method
print(set_a ^ set_b) # Output: {1, 2, 4, 5}
print(set_a.symmetric_difference(set_b)) # Output: {1, 2, 4, 5}
# Subset using the '<=' operator or the issubset() method
print(set_a <= set_b) # Output: False
print(set_a.issubset(set_b)) # Output: False
# Superset using the '>=' operator or the issuperset() method
print(set_a >= set_b) # Output: False
print(set_a.issuperset(set_b)) # Output: False
Frozen Sets
A frozen set is a type of set that is immutable, meaning you cannot add or remove elements from it once it is created. Frozen sets can be useful for storing sets of sets or as keys in dictionaries because they are hashable:
# Creating a frozen set
my_frozen_set = frozenset([1, 2, 3])
# Attempting to add an element to the frozen set (will raise a TypeError)
my_frozen_set.add(4)
To convert a regular set to a frozen set, you can use the
frozenset()
constructor:
# Creating a regular set
my_set = {1, 2, 3}
# Converting the set to a frozen set
my_frozen_set = frozenset(my_set)
Like regular sets, frozen sets support the same set operations and can be used in the same way.
Conclusion
Sets are a useful data structure in Python for storing collections of unique elements. They have a variety of built-in methods and support set operations like union, intersection, difference, and more. Frozen sets are a special type of set that is immutable and can be useful in certain situations where you need a hashable collection of unique elements.
Now that you know the basics of sets in Python, you can start incorporating them into your own programs and take advantage of their powerful features!