In Python, a set is a collection of unique items. A set does not allow duplicate values, and it is unordered, meaning the items in a set are not stored in any specific order. Sets are used when you want to store multiple items, but each item should only appear once.
You can create a set in Python using curly braces {}
or the set()
function.
Creating a Set
Here’s how to create a set:
# Creating a set using curly braces
fruits = {"apple", "banana", "cherry"}
print(fruits)
Output:
{'banana', 'cherry', 'apple'}
Notice that the order of items can be different every time you print a set. This is because sets are unordered.
Adding Items to a Set
To add items to a set, we use the add()
method. This method adds a single item to the set.
# Adding an item to the set
fruits.add("orange")
print(fruits)
Output:
{'banana', 'cherry', 'apple', 'orange'}
If you try to add an item that already exists in the set, Python will simply ignore it.
fruits.add("apple")
print(fruits)
Output:
{'banana', 'cherry', 'apple', 'orange'}
Notice that the item “apple” was not added again because sets don’t allow duplicates.
Removing Items from a Set
There are several methods to remove items from a set:
remove()
: Removes a specific item. If the item doesn’t exist in the set, it raises an error.
# Removing an item from the set
fruits.remove("banana")
print(fruits)
Output:
{'cherry', 'apple', 'orange'}
discard()
: Removes an item, but does not raise an error if the item is not found.
# Discarding an item from the set
fruits.discard("grape") # "grape" doesn't exist in the set
print(fruits)
Output:
{'cherry', 'apple', 'orange'}
pop()
: Removes a random item from the set. Since sets are unordered, you don’t know which item will be removed.
# Popping a random item from the set
removed_item = fruits.pop()
print(f"Removed item: {removed_item}")
print(fruits)
Output:
Removed item: cherry
{'apple', 'orange'}
Clearing a Set
If you want to remove all items from a set, you can use the clear()
method.
# Clearing all items from the set
fruits.clear()
print(fruits)
Output:
set()
Copying a Set
If you want to create a copy of a set (so that changing the copy doesn’t affect the original), you can use the copy()
method.
# Copying the set
fruits = {"apple", "banana", "cherry"}
fruits_copy = fruits.copy()
print(fruits_copy)
Output:
{'banana', 'cherry', 'apple'}
Set Operations
Python sets support mathematical set operations like union, intersection, and difference.
Union (|
)
The union operation combines two sets and returns all unique elements.
# Union of two sets
set1 = {"apple", "banana"}
set2 = {"banana", "cherry", "date"}
union_set = set1 | set2
print(union_set)
Output:
{'cherry', 'banana', 'apple', 'date'}
Intersection (&
)
The intersection operation returns only the items that are common in both sets.
# Intersection of two sets
intersection_set = set1 & set2
print(intersection_set)
Output:
{'banana'}
Difference (-
)
The difference operation returns items that are in the first set but not in the second.
# Difference of two sets
difference_set = set1 - set2
print(difference_set)
Output:
{'apple'}
Symmetric Difference (^
)
The symmetric difference returns items that are in either of the sets, but not in both.
# Symmetric difference of two sets
sym_diff_set = set1 ^ set2
print(sym_diff_set)
Output:
{'cherry', 'apple', 'date'}
Set Length
You can find the number of items in a set using the len()
function.
# Getting the length of a set
set_length = len(fruits)
print(set_length)
Output:
3
Checking if an Item Exists in a Set
You can check if an item is in a set using the in
keyword.
# Checking if an item exists in the set
is_apple_present = "apple" in fruits
print(is_apple_present)
Output:
True