Python Sets are data structures that hold a collection with no duplicates. It is a great way to keep track of related data. This guide shows you how to get started. First, we will cover the basics, such as creating and manipulating sets and move on to more advanced concepts, such as set operations.
Let's get started!
Creating a Set
Creating a set is done by using the set()
function.
Example
You could create a set of integers like this:
my_set = set([0,1, 1, 2,])
print(my_set)
Output:
{0,1,2}
As you can see, the output is enclosed in curly braces {}. It indicates that the data is stored in a set. The order of the elements does not matter, and duplicate values are not allowed. It will be ignored if you try to insert a duplicate value into a set. Let's take a look at another example:
Example
my_set = set(["a", "b", "c"])
print(my_set)
Output:
{'a', 'b', 'c'}
As you can see, you can store any data type in a set. In the example above, we have stored strings.
Mixing Data Types in Python Sets
However, you can also mix and match different data types in the same set.
For example:
my_set = set([0, "a", True])
print(my_set)
Output:
{0, 'a', True}
Now that we know how to create sets let's move on to manipulating them.
Manipulating Python Sets
There are a few ways to manipulate sets.
Adding Elements to a set
The first way is to add elements to a set. It can be done using the add()
method.
For example:
my_set = set()
my_set.add(0)
my_set.add("a")
print(my_set)
Output:
{0, 'a'}
As you can see, we created an empty set and then added two elements to it. We can add as many elements possible to a set.
Removing Elements from a Python Set
It can be done using the remove()
or pop()
method.
For example:
my_set = set([0, "a", True])
my_set.remove("a") # Remove "a" from the set
print(my_set)
Output:
{0, True}
Example 2:
my_set = set([0, "a", True])
my_set.pop() # Remove an arbitrary element from the set
print(my_set)
Output:
{"a",True}
The remove()
method removes a specific element from the set, while the pop()
method removes an arbitrary element. If you try to remove an element that doesn't exist in the set, you will get an error.
Clearing a Python set
You can also clear a set using the clear()
method. This method will remove all of the elements from the set:
my_set = set([0, "a", True])
my_set.clear() # Clear the set
print(my_set)
Output:
Set ()
As you can see, the set is now empty.
Now that we know how to create and manipulate sets, let's move on to more advanced concepts.
Set Operations
One useful thing you can do with sets is to perform operations on them.
Sets Union
For example, you can find the union of two sets using the union()
method. The union of two sets is the set of all elements that are in either set.
For example:
my_set = set([0, "a", True])
other_set = set([False, "b"])
print(my_set.union(other_set)) # Find the union of two sets
Output:
{0, 'a', True, False, 'b'}
Example 2:
my_set = set([0, "a", True])
other_set = set([False, "b"])
print(my_set | other_set)
Output:
{0, True, 'a', 'b'}
As you can see, the union of the two sets contains all of the elements from both sets.
Sets Intersection
You can also find the intersection of two sets using the intersection()
method. The intersection of two sets is the set of all elements in both sets.
For example:
my_set = set([0, "a", True, 1])
other_set = set(["a", "b", 1])
print(my_set.intersection(other_set)) # Find the intersection of two sets
Output:
{1, 'a'}
As you can see, the intersection of the two sets is empty because there are no elements in both sets.
Sets Difference
You can also find differences between two sets using the difference()
method. The difference between two sets is the set of all elements that are in one set but not the other.
For example:
my_set = set([0, "a", True])
other_set = set([False, "b"])
print(my_set.difference(other_set)) # Find the difference between two sets
Output:
{'a', True}
There are a few other set operations, but these are the most commonly used.
Conclusion
That's all for now! I hope this guide helped get you started with Python sets. If you have any questions, feel free to post them in the comments below.
Thanks for reading!