In Python, a set is an unordered collection of unique elements. Sets are mutable, meaning that you can add or remove elements after the set is created. However, sets do not allow duplicate elements, and they do not maintain any order for their elements.
1. Creating Sets
You can create a set by placing all the elements inside curly braces {}
or by using the set()
function.
# Creating an empty set
empty_set = set()
# Creating a set with elements
my_set = {1, 2, 3, 4, 5}
# Creating a set with mixed data types
mixed_set = {1, “Hello”, 3.14, True}
# Creating a set from a list
list_set = set([1, 2, 3, 4, 5])
Note: To create an empty set, you must use the set()
function. Using {}
creates an empty dictionary, not a set.
2. Characteristics of Sets
- Unordered: Sets do not maintain the order of elements.
- No Duplicates: Sets automatically remove duplicate elements.
- Mutable: You can add or remove elements from a set, but the set itself cannot contain mutable elements like lists or other sets.
my_set = {1, 2, 3, 4, 5, 3, 2}
print(my_set) # Output: {1, 2, 3, 4, 5} - Duplicates are removed
3. Accessing Elements in a Set
You cannot access elements of a set by index or key because sets are unordered. However, you can iterate through the elements using a for
loop.
my_set = {"apple", "banana", "cherry"}
# Iterating through a set
for fruit in my_set:
print(fruit)
Output:
apple
banana
cherry
4. Adding and Removing Elements
a. Adding Elements
add(x)
: Adds a single elementx
to the set.
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
update(iterable)
: Adds multiple elements from an iterable (like a list or another set) to the set.
my_set.update([5, 6, 7])
print(my_set) # Output: {1, 2, 3, 4, 5, 6, 7}
b. Removing Elements
remove(x)
: Removes the elementx
from the set. Raises aKeyError
ifx
is not in the set.
my_set.remove(2)
print(my_set) # Output: {1, 3, 4, 5, 6, 7}
discard(x)
: Removes the elementx
from the set. Does nothing ifx
is not in the set (no error is raised).
my_set.discard(10) # No error, even though 10 is not in the set
pop()
: Removes and returns an arbitrary element from the set. Raises aKeyError
if the set is empty.
element = my_set.pop()
print(element) # Output: (some element from the set)
print(my_set) # Output: Set after one element is removed
clear()
: Removes all elements from the set, leaving it empty.
my_set.clear()
print(my_set) # Output: set()
5. Set Operations
Python sets support various operations that are useful for mathematical set operations like union, intersection, difference, and symmetric difference.
a. Union
The union of two sets is a set containing all elements from both sets, without duplicates.
- Syntax:
set1 | set2
orset1.union(set2)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set) # Output: {1, 2, 3, 4, 5}
b. Intersection
The intersection of two sets is a set containing only the elements that are common to both sets.
- Syntax:
set1 & set2
orset1.intersection(set2)
intersection_set = set1 & set2
print(intersection_set) # Output: {3}
c. Difference
The difference of two sets is a set containing elements that are in the first set but not in the second set.
- Syntax:
set1 - set2
orset1.difference(set2)
difference_set = set1 - set2
print(difference_set) # Output: {1, 2}
d. Symmetric Difference
The symmetric difference of two sets is a set containing elements that are in either of the sets, but not in both.
- Syntax:
set1 ^ set2
orset1.symmetric_difference(set2)
sym_diff_set = set1 ^ set2
print(sym_diff_set) # Output: {1, 2, 4, 5}
6. Subset and Superset Operations
- Subset: A set
A
is a subset of another setB
if all elements ofA
are also inB
.- Syntax:
set1 <= set2
orset1.issubset(set2)
- Syntax:
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1 <= set2) # Output: True
Superset: A set A
is a superset of another set B
if it contains all elements of B
.
- Syntax:
set1 >= set2
orset1.issuperset(set2)
print(set2 >= set1) # Output: True
7. Set Methods
Here are some commonly used set methods in Python:
add(x)
: Adds an elementx
to the set.update(iterable)
: Adds multiple elements to the set.remove(x)
: Removes an elementx
from the set (raisesKeyError
if not found).discard(x)
: Removes an elementx
from the set (does nothing if not found).pop()
: Removes and returns an arbitrary element from the set.clear()
: Removes all elements from the set.union(*others)
: Returns a new set with elements from the set and all others.intersection(*others)
: Returns a new set with elements common to the set and all others.difference(*others)
: Returns a new set with elements in the set that are not in the others.symmetric_difference(other)
: Returns a new set with elements in either the set orother
but not both.issubset(other)
: ReturnsTrue
if the set is a subset ofother
.issuperset(other)
: ReturnsTrue
if the set is a superset ofother
.copy()
: Returns a shallow copy of the set.
8. Iterating Over a Set
You can iterate over the elements of a set using a for
loop.
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element)
Output:
1
2
3
4
5
9. Frozen Sets
A frozen set is an immutable version of a set. Once created, elements cannot be added or removed. Frozen sets are hashable, so they can be used as keys in dictionaries or elements of other sets.
- Creating a Frozen Set
frozen_set = frozenset([1, 2, 3, 4, 5])
print(frozen_set) # Output: frozenset({1, 2, 3, 4, 5})
10. Common Use Cases of Sets
- Removing Duplicates: Sets automatically remove duplicate elements, making them useful for filtering unique values.
numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = set(numbers)
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
- Membership Testing: Sets are optimized for fast membership testing.
my_set = {1, 2, 3, 4, 5}
print(3 in my_set) # Output: True
print(10 in my_set) # Output: False
- Set Operations: Performing mathematical set operations like union, intersection, difference, and symmetric difference.
Python sets are a powerful and flexible data structure, particularly useful for storing unique elements and performing operations like union, intersection, and difference. Their immutability and lack of order make them different from other collections like lists or tuples, but they are highly efficient for certain tasks, especially when dealing with large collections of data or when membership testing is required.