In Python, a set is a collection of unique items. You can think of a set like a box that only holds distinct items. If you try to add the same item again, it won’t be stored.
Key Features of Sets:
- Unique: No duplicates allowed.
- Unordered: The items don’t follow any specific order.
- Mutable: You can change the set after it’s created.
Creating Sets
To create a set, you use curly braces {}
or the set()
function.
Example 1: Creating a Set Using Curly Braces
fruits = {"apple", "banana", "cherry"}
print(fruits)
Output:
{'banana', 'apple', 'cherry'}
Notice that the order of the items may vary when printed. That’s because sets are unordered.
Example 2: Creating a Set Using the set()
Function
numbers = set([1, 2, 3, 4, 5])
print(numbers)
Output:
{1, 2, 3, 4, 5}
Adding Items to a Set
You can add new items to a set using the add()
method.
Example 3: Adding an Item
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
Output:
{'banana', 'apple', 'cherry', 'orange'}
Removing Items from a Set
To remove an item from a set, use the remove()
or discard()
method.
remove()
will throw an error if the item does not exist.discard()
will not throw an error if the item does not exist.
Example 4: Removing an Item with remove()
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
Output:
{'apple', 'cherry'}
Example 5: Removing an Item with discard()
fruits = {"apple", "banana", "cherry"}
fruits.discard("orange") # No error even though 'orange' is not in the set
print(fruits)
Output:
{'banana', 'apple', 'cherry'}
Set Operations
Sets also support various operations that allow you to combine or compare sets.
Union: Combining Two Sets
You can combine two sets using the |
operator or the union()
method.
Example 6: Union of Sets
fruits = {"apple", "banana", "cherry"}
more_fruits = {"orange", "grape", "apple"}
combined_fruits = fruits | more_fruits
print(combined_fruits)
Output:
{'banana', 'grape', 'cherry', 'orange', 'apple'}
Notice that the duplicate “apple” is only stored once in the result.
Intersection: Finding Common Items
The intersection of two sets gives you the items that are in both sets. You can use the &
operator or the intersection()
method.
Example 7: Intersection of Sets
fruits = {"apple", "banana", "cherry"}
more_fruits = {"orange", "grape", "apple"}
common_fruits = fruits & more_fruits
print(common_fruits)
Output:
{'apple'}
Difference: Items Only in One Set
The difference of two sets gives you the items that are in the first set but not in the second. You can use the -
operator or the difference()
method.
Example 8: Difference of Sets
fruits = {"apple", "banana", "cherry"}
more_fruits = {"orange", "grape", "apple"}
exclusive_fruits = fruits - more_fruits
print(exclusive_fruits)
Output:
{'banana', 'cherry'}
Why Use Sets?
Sets are useful when you want to:
- Ensure that a collection has no duplicates.
- Perform mathematical set operations like union, intersection, and difference.
- Check membership (whether an item is in the set) efficiently.