Sets are data structures that allow us to store collections of unique and unordered elements.
This means that there can’t be duplicates in a set and the elements are not ordered by position.
Characteristics of sets:
- Unique elements: Sets cannot contain duplicate elements, so each element is unique.
- Unordered: Elements in a set have no specific order and there is no guarantee that the order in which they were added will be maintained.
If you want to learn more
consult the Introduction to Programming Course read more ⯈
Creating Sets
Sets can be created using curly braces {}
or the set()
function.
Here is the text with explanation lines added between each heading and the corresponding example:
Create a Set with Curly Braces {}
Sets in Python can be created using curly braces {}
. In this case, the elements separated by commas inside the braces are listed to initialize the set.
my_set = {1, 2, 3}
Create a Set with the set()
Function
It is also possible to create sets using the set()
function. A list is passed as an argument to the set()
function, and the function creates a set with the elements of that list.
my_other_set = set([1, 2, 3])
For example, here we have created a Set {1, 2, 3}
from a List containing [1, 2, 3]
.
Set Operations
Adding elements
Sets in Python have the add()
method, which is used to add a single element to the set.
my_set.add(6) # Add the element 6 to the set
Removing elements
Sets in Python have the remove()
method, which is used to remove a specific element from the set. Additionally, the discard()
method can also be used to remove an element, but it will not throw an error if the element is not present in the set.
my_set.remove(3) # Remove the element 3 from the set
my_set.discard(2) # Remove the element 2 if it is present
Set Union
The union of sets can be performed using the union()
method or the |
operator. This creates a new set containing all the elements from both original sets, eliminating duplicates.
union_set = my_set.union(my_other_set) # Merge the two sets into a new one
union_set = my_set | my_other_set # The same using the operator |
Set Intersection
The intersection of sets can be performed using the intersection()
method or the &
operator. This creates a new set containing only the elements that are present in both original sets.
intersection_set = my_set.intersection(my_other_set) # Get the intersection of the sets
intersection_set = my_set & my_other_set # The same using the operator &
Set Difference
The difference between sets can be calculated using the difference()
method or the -
operator. This creates a new set containing only the elements that are present in the first set but not in the second.
difference_set = my_set.difference(my_other_set) # Get the difference between the sets
difference_set = my_set - my_other_set # The same using the operator -
Examples of Set Usage
- Removing Duplicates in Lists:
To remove duplicates from a list in Python, the list can be converted to a set using the set()
function.
list_with_duplicates = [1, 2, 3, 4, 1, 2, 5]
set_without_duplicates = set(list_with_duplicates)
Sets in Python do not allow duplicate elements, so when the list is converted to a set, duplicates are automatically removed.
- Membership Check:
To check if an element is present in a set, the in
expression can be used. This expression returns True
if the element is present in the set and False
if it is not.
if 3 in my_set:
print("Element 3 is present in the set.")
- Set Operations:
Python offers several built-in methods for performing set operations, such as union, intersection, and difference.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
# Set union
union = set1.union(set2) # Result: {1, 2, 3, 4, 5}
# Set intersection
intersection = set1.intersection(set2) # Result: {3}
# Set difference
difference = set1.difference(set2) # Result: {1, 2}
These operations can be performed using set methods such as union()
, intersection()
, and difference()
, as we have seen earlier.