A set is an unordered collection of unique and immutable elements in Python. It is defined by placing elements within curly braces {}
or by using the built-in set()
function.
- Unordered: The elements do not have a defined order.
- Unique: No duplicate elements are allowed.
- Mutable: You can add or remove elements from a set.
Sets are used in Python for various tasks that require handling unique items and performing operations like:
- 🔍 Removing duplicates from a list.
- ✅ Checking membership (whether an element is in a set).
- ➕ Performing mathematical operations like union, intersection, difference, and symmetric difference.
- Uniqueness: Sets automatically handle uniqueness, making them perfect for filtering duplicates.
- Efficient Membership Testing: Sets provide O(1) average-time complexity for membership tests.
- Mathematical Operations: Sets are ideal for performing union, intersection, and difference operations.
# Creating a set with curly braces
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
# Creating a set from a list using the set() constructor
my_set = set([1, 2, 3, 3, 4, 4, 5])
print(my_set) # Output: {1, 2, 3, 4, 5}
# Creating an empty set
empty_set = set()
print(empty_set) # Output: set()
Python provides several built-in methods for sets. Here’s a detailed list of all set methods with examples:
# 🍎 Adding an element to the set
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits) # Output: {'apple', 'banana', 'cherry', 'orange'}
- 📝 Explanation: The
add()
method is used to add a single element to a set. Since sets do not allow duplicates, if the element already exists, it will not be added again.
# 🧹 Clearing all elements from the set
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits) # Output: set()
- 📝 Explanation: The
clear()
method removes all the elements from the set, resulting in an empty set.
# 📋 Making a shallow copy of the set
fruits = {"apple", "banana", "cherry"}
new_fruits = fruits.copy()
print(new_fruits) # Output: {'apple', 'banana', 'cherry'}
- 📝 Explanation: The
copy()
method creates a shallow copy of the set. This means a new set is created, but the elements are references to the same objects in memory.
A shallow copy creates a new set object but does not create copies of the elements contained in the original set; instead, it references them. If the elements are immutable (like strings, integers), a shallow copy behaves the same as a deep copy. However, if the set contains mutable objects (like lists or dictionaries), changes to those mutable objects will be reflected in both the original and the copied set.
# ➖ Difference between sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.difference(set2)
print(result) # Output: {1, 2}
- 📝 Explanation: The
difference()
method returns a new set containing elements that are in the first set but not in the second set.
5. difference_update()
: Removes the items in this set that are also included in another specified set.
# 🔄 Updating set1 by removing common elements
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.difference_update(set2)
print(set1) # Output: {1, 2}
- 📝 Explanation: The
difference_update()
method removes all elements of the set that are also present in another specified set.
# 🚫 Discarding an element
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits) # Output: {'apple', 'cherry'}
fruits.discard("pear") # Does nothing since "pear" is not in the set
- 📝 Explanation: The
discard()
method removes the specified element from the set without raising an error if the element is not found.
# ⛓️ Intersection of sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.intersection(set2)
print(result) # Output: {3, 4}
- 📝 Explanation: The
intersection()
method returns a new set that contains only the common elements from both sets.
# 🔄 Updating set1 to its intersection with set2
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.intersection_update(set2)
print(set1) # Output: {3, 4}
- 📝 Explanation: The
intersection_update()
method updates the set with the intersection of itself and another set.
# ❌ Checking if sets are disjoint
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(set1.isdisjoint(set2)) # Output: True
- 📝 Explanation: The
isdisjoint()
method returnsTrue
if two sets have no elements in common.
# ✔️ Checking if set1 is a subset of set2
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
print(set1.issubset(set2)) # Output: True
- 📝 Explanation: The
issubset()
method checks if all elements of the set are present in another set.
# ✔️ Checking if set1 is a superset of set2
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
print(set1.issuperset(set2)) # Output: True
- 📝 Explanation: The
issuperset()
method checks if the set contains all elements of another set.
# 🔄 Popping an element from the set
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()
print(item) # Output: Random element (e.g., 'banana')
print(fruits) # Output: Remaining set without
the popped element
- 📝 Explanation: The
pop()
method removes and returns a random element from the set. Since sets are unordered, there is no guarantee which element will be removed.
13. remove()
: Removes the specified element from the set. If the element is not found, it raises a KeyError
.
# 🗑️ Removing an element from the set
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits) # Output: {'apple', 'cherry'}
# fruits.remove("pear") # Raises KeyError
- 📝 Explanation: The
remove()
method removes the specified element from the set. If the element is not present, it raises aKeyError
.
14. symmetric_difference()
: Returns a set with elements in either the set or the specified set, but not in both.
# 🔄 Symmetric difference between sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2)
print(result) # Output: {1, 2, 4, 5}
- 📝 Explanation: The
symmetric_difference()
method returns a set containing elements that are in either set but not in both.
15. symmetric_difference_update()
: Updates the set with the symmetric difference of itself and another.
# 🔄 Updating set1 to its symmetric difference with set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.symmetric_difference_update(set2)
print(set1) # Output: {1, 2, 4, 5}
- 📝 Explanation: The
symmetric_difference_update()
method updates the set with the symmetric difference of itself and another set.
16. union()
: Returns a set that contains all items from the original set and all items from the specified sets.
# 🔗 Union of two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result) # Output: {1, 2, 3, 4, 5}
- 📝 Explanation: The
union()
method returns a new set containing all the unique elements from both sets.
# 🔄 Updating set1 with union of set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1) # Output: {1, 2, 3, 4, 5}
- 📝 Explanation: The
update()
method adds all elements from another set (or any iterable) to the set.
Sets are powerful data structures that provide an efficient way to handle collections of unique items. They are particularly useful in situations where duplicates need to be avoided, and various mathematical set operations are required. With this comprehensive guide and examples, you now have a solid understanding of Python sets, their capabilities, and how shallow copies work.