Set & Set Operation in Python
Sets are a unique and powerful data structure in Python that allow you to store collections of items without duplicates. They are particularly useful when you need to perform operations involving unique elements, such as union, intersection, and difference. In this article, we’ll dive into the world of Python sets, understand their properties, and see how they can be used effectively with examples.
1. What is a Set?
A set is an unordered collection of unique elements. Unlike lists or tuples, sets do not maintain the order of elements, and they automatically handle duplicates by only keeping one instance of each element.
# Creating a set
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}2. Creating Sets
You can create sets using curly braces {} or the set() function.
Using Curly Braces:
# Creating a set with curly braces
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'banana', 'cherry', 'apple'}Using the set() Function:
# Creating an empty set
empty_set = set()
print(empty_set) # Output: set()
# Creating a set from a list
numbers = set([1, 2, 3, 4, 5])
print(numbers) # Output: {1, 2, 3, 4, 5}3. Adding and Removing Elements
Sets are mutable, meaning you can add or remove elements after creation.
Get Lavdev Awasthi’s stories in your inbox
Join Medium for free to get updates from this writer.
Adding Elements:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}Removing Elements:
my_set = {1,2,3,4}
my_set.remove(2)
print(my_set) # Output: {1, 3, 4}
# Using discard() to remove an element that may or may not be present
my_set.discard(5) # No error if 5 is not present
print(my_set) # Output: {1, 3, 4}
# Using pop() to remove and return an arbitrary element
removed_element = my_set.pop()
print(removed_element) # Output: 1 (or any other element)
print(my_set) # Output: {3, 4}4. Set Operations
Python sets support several operations that make them powerful for handling collections of unique elements.
- Union: Combines elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}- Intersection: Returns elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set) # Output: {3}- Difference: Returns elements that are in the first set but not in the second.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set) # Output: {1, 2}- Symmetric Difference: Returns elements that are in either of the sets but not in both.
symmetric_diff_set = set1.symmetric_difference(set2)
print(symmetric_diff_set) # Output: {1, 2, 4, 5}5. Set Methods
Sets come with several built-in methods for common tasks.
- issubset(): Checks if a set is a subset of another set.
subset = {1, 2}
print(subset.issubset(set1)) # Output: True- issuperset(): Checks if a set is a superset of another set.
subset = {1, 2}
print(set1.issuperset(subset)) # Output: True- isdisjoint(): Checks if two sets have no elements in common.
subset = {1, 2}
print(set1.isdisjoint(set2)) # Output: False6. Iterating Over Sets
You can iterate over the elements of a set using a for loop.
Example:
my_set = {"apple", "banana", "cherry"}
for fruit in my_set:
print(fruit)7. Set Comprehensions
Similar to list comprehensions, you can create sets using set comprehensions.
Example:
# Creating a set of squares using set comprehension
squares = {x ** 2 for x in range(10)}
print(squares) # Output: {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}Conclusion
Sets are a powerful and versatile data structure in Python, offering unique capabilities for handling collections of unique elements. By understanding how to create, modify, and perform operations with sets, you can effectively manage and manipulate data in your Python programs.
Happy coding! 🧑💻
Feel free to ask if you have any other questions or need further assistance!







