Python is a powerful programming language that offers a wide range of data structures to store and manipulate data. One such data structure is the set. In this article, we will explore what a set is and how it can be used in Python.
What is a Set?
In Python, a set is an unordered collection of unique elements. This means that duplicate values are not allowed in a set. Sets are mutable, which means you can add or remove elements from them.
Creating a Set
To create a set in Python, you can use curly braces ({}) or the built-in set() function. Let’s take a look at some examples:
Example 1:
“`python
my_set = {1, 2, 3}
print(my_set)
“`
Output:
“`
{1, 2, 3}
“`
Example 2:
“`python
my_set = set([4, 5, 6])
print(my_set)
“`
Output:
“`
{4, 5, 6}
“`
In both examples, we created sets with three elements.
Adding Elements to a Set
You can add elements to a set using the .add() method. Let’s see an example:
“`python
fruits = {‘apple’, ‘banana’, ‘orange’}
fruits.add(‘mango’)
print(fruits)
“`
Output:
“`
{‘apple’, ‘banana’, ‘orange’, ‘mango’}
“`
In this example, we added the element ‘mango’ to the fruits set using the .
Removing Elements from a Set
To remove an element from a set, you can use the .remove() method or the .discard() method. The difference between these methods is that .remove() raises an error if the element is not found in the set, while .discard() does not.
Example 1:
“`python
fruits = {‘apple’, ‘banana’, ‘orange’}
fruits.remove(‘banana’)
print(fruits)
“`
Output:
“`
{‘apple’, ‘orange’}
“`
Example 2:
“`python
fruits = {‘apple’, ‘banana’, ‘orange’}
fruits.discard(‘mango’)
print(fruits)
“`
Output:
“`
{‘apple’, ‘banana’, ‘orange’}
“`
In Example 1, we removed the element ‘banana’ from the fruits set using the .remove() method. In Example 2, we tried to remove the non-existent element ‘mango’ using the .discard() method, but it didn’t raise an error.
Set Operations
Sets support various operations such as union, intersection, difference, and symmetric difference.
- Union: Returns a new set containing all elements from both sets.
- Intersection: Returns a new set containing common elements between two sets.
- Difference: Returns a new set containing elements that are only in one of the sets.
- Symmetric Difference: Returns a new set containing elements that are present in either of the sets, but not in both.
Here’s an example that demonstrates these operations:
“`python
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
intersection_set = set1.intersection(set2)
difference_set = set1.difference(set2)
symmetric_difference_set = set1.symmetric_difference(set2)
print(“Union:”, union_set)
print(“Intersection:”, intersection_set)
print(“Difference:”, difference_set)
print(“Symmetric Difference:”, symmetric_difference_set)
“`
Output:
“`
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference: {1, 2}
Symmetric Difference: {1, 2, 4, 5}
“`
In this example, we performed various operations on two sets – set1 and set2.
Conclusion
Sets are a useful data structure in Python when you want to store a collection of unique elements. They provide methods to add or remove elements and perform various operations like union, intersection, difference, and symmetric difference.
Being an unordered collection, sets are handy when order doesn’t matter. Remember that sets do not allow duplicate values.
Now that you have a good understanding of sets in Python, you can leverage them in your programs to manage unique data efficiently.