Python set is a data structure that allows you to work with collections of unique elements. Sets are useful for tasks such as removing duplicates from a list, testing membership of an element, and performing set operations such as union and intersection. In Python, sets are mutable and iterable, making them versatile and easy to work with. Let's explore the basics of Python sets as well as main python set methods to help you make the most of this data structure in your programming projects.
What is a Set?
A set is a collection of unique elements, much like a mathematical set. A set is an unordered and mutable collection of distinct elements enclosed in curly braces {}. Sets can contain any hashable elements such as integers, floats, strings, and even other sets.
For example, you can create a set of integers as follows:
my_set = {1, 2, 3, 4, 5}
Or, you can create a set of strings as follows:
my_set = {'apple', 'banana', 'orange', 'grape'}
You can perform various operations on sets, such as adding or removing elements, finding the intersection or union of sets, or checking if an element is a member of a set.
You can use Python's set operations and methods for data manipulation. Here are some of the most commonly used ones:
Set Operations
-
union()
or|
: Returns a set containing all the elements from both sets (or other iterable objects), with duplicates removed. Example:set1 = {1, 2, 3}; set2 = {3, 4, 5}; set3 = set1.union(set2); print(set3)
will output{1, 2, 3, 4, 5}
. -
intersection()
or&
: Returns a set containing only the elements that are common to both sets. Example:set1 = {1, 2, 3}; set2 = {3, 4, 5}; set3 = set1.intersection(set2); print(set3)
will output{3}
. -
difference()
or-
: Returns a set containing the elements that are in the first set but not in the second set. Example:set1 = {1, 2, 3}; set2 = {3, 4, 5}; set3 = set1.difference(set2); print(set3)
will output{1, 2}
. -
symmetric_difference()
or^
: Returns a set containing the elements that are in either of the sets but not in both. Example:set1 = {1, 2, 3}; set2 = {3, 4, 5}; set3 = set1.symmetric_difference(set2); print(set3)
will output{1, 2, 4, 5}
. -
issubset()
: Returns True if all the elements of one set are contained in the other set. Example:set1 = {1, 2, 3}; set2 = {1, 2}; print(set2.issubset(set1))
will outputTrue
. -
issuperset()
: Returns True if a set contains all the elements of another set. Example:set1 = {1, 2, 3}; set2 = {1, 2}; print(set1.issuperset(set2))
will outputTrue
.
Set Methods
-
add()
: Adds an element to the set. If the element already exists in the set, the set remains unchanged. Example:my_set = {1, 2, 3}; my_set.add(4); print(my_set)
will output{1, 2, 3, 4}
. -
remove()
: Removes the specified element from the set. Raises a KeyError if the element is not in the set. Example:my_set = {1, 2, 3}; my_set.remove(2); print(my_set)
will output{1, 3}
. -
discard()
: Removes the specified element from the set. Does not raise an error if the element is not in the set. Example:my_set = {1, 2, 3}; my_set.discard(2); print(my_set)
will output{1, 3}
. -
pop()
: Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty. Example:my_set = {1, 2, 3}; x = my_set.pop(); print(my_set, x)
will output{2, 3}, 1
. -
clear()
: Removes all elements from the set. Example:my_set = {1, 2, 3}; my_set.clear(); print(my_set)
willoutput set()
.
Next we will look at the most commonly used in detail.
Creating Sets in Python
In Python, an empty set can be created using the set()
function. Here's an example:
empty_set = set()
To initialize a set with values, you can use a set literal. Here's a set example:
my_set = {1, 2, 3}
As above, you can create a set in Python by using a set literal or the set()
function.
How to Add to a Set in Python
In Python, you can add to a set an element using the add()
method. Here's an example of adding to a set:
my_set = {1, 2, 3} # create a set with initial values
my_set.add(4) # add a new value to the set
print(my_set) # output: {1, 2, 3, 4}
Note: set objects do not have an
append()
method since they are unordered collections of unique elements.
Removing an Element to a Set
In Python, the set
data type represents a collection of unique elements. The remove
method is used to remove a specific element from a set.
Here is an example of how to remove from a set:
# create a set
my_set = {1, 2, 3, 4, 5}
# remove an element from the set
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5}
It's important to note that if the element to be removed is not in the set, the remove
method will raise a KeyError
exception. If you want to avoid this, you can use the discard
method instead, which removes an element from the set if it is present, and does nothing if the element is not in the set.
Here's an example of using the discard method in Python:
# create a set
my_set = {1, 2, 3, 4, 5}
# discard an element from the set
my_set.discard(3)
print(my_set) # Output: {1, 2, 4, 5}
# try to discard an element that is not in the set
my_set.discard(10)
print(my_set) # Output: {1, 2, 4, 5}
Access to Set Elements
Sets are unordered collections of unique elements. Therefore, they do not support indexing by integer values like lists or tuples.
If you need to access a specific element in a set, you can use the in
keyword to check if python set contains an element, or you can use the for
loop to iterate over the elements of the set.
Here are some examples:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Checking if an element exists in the set
if 3 in my_set:
print("3 is in the set")
# Iterating over the elements of the set
for element in my_set:
print(element)
Output:
3 is in the set
1
2
3
4
5
Python's Set Length
In Python, you can use the built-in len()
function to get the number of elements in a set.
For example, consider the following set:
my_set = {1, 2, 3, 4, 5}
print(len(my_set)) # Output: 5
Sorting a Set in Python
Sets are unordered collections of unique elements, which means the order of elements is not preserved in a set, so final output can not be a sorted set.
In Python, you can sort a set using the built-in sorted()
function or the sort()
method. So using this method we convert a set to a list.
Using sorted()
function:
my_set = {4, 1, 3, 2, 5}
sorted_set = sorted(my_set)
print(sorted_set) # Output: [1, 2, 3, 4, 5]
When sorted()
is applied to a set, it returns a new sorted list containing all the elements of the original set.
Using sort()
method:
my_set = {4, 1, 3, 2, 5}
sorted_list = list(my_set)
sorted_list.sort()
sorted_set = set(sorted_list)
print(sorted_set) # Output: {1, 2, 3, 4, 5}
Note: the
sort()
method is only available for lists and cannot be used directly with a set. So, you need to first convert the set to a list, then use thesort()
method, and then convert it back to a set.
In both cases, the resulting set will be sorted in ascending order. If you want to sort in descending order of a set, you can use the reverse=True
argument in sorted()
function or the sort()
method's reverse parameter.
my_set = {4, 1, 3, 2, 5}
sorted_set_desc = sorted(my_set, reverse=True)
print(sorted_set_desc) # Output: [5, 4, 3, 2, 1]
my_set = {4, 1, 3, 2, 5}
sorted_list_desc = list(my_set)
sorted_list_desc.sort(reverse=True)
sorted_set_desc = set(sorted_list_desc)
print(sorted_set_desc) # Output: {5, 4, 3, 2, 1}
The update Method for Set in Python
The update
method of a set allows you to add multiple elements to a set at once.
Here is the syntax for using the update method:
set.update(iterable)
The iterable
argument can be any iterable object, such as a list, tuple, set, or even a string. The update
method adds all the elements in the iterable
to the set, while removing any duplicates.
Here's an example that demonstrates the update method:
>>> s1 = {1, 2, 3}
>>> s2 = {3, 4, 5}
>>> s1.update(s2)
>>> print(s1)
{1, 2, 3, 4, 5}
Intersection of Sets in Python
In Python intersection of sets can be found using the intersection()
method.
Let's see how to get intersection of two sets in Python:
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set3 = {4, 5, 6, 7}
intersection_set = set1.intersection(set2, set3)
print(intersection_set) # Output: {4}
Sets' Difference in Python
The difference of sets in Python ca be find using the "-" operator or the difference()
method. Here's an example:
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Using the "-" operator
diff = set1 - set2
print(diff) # Output: {1, 2}
# Using the `difference()` method
diff = set1.difference(set2)
print(diff) # Output: {1, 2}
Sets' Union in Python
Uion of sets in Python can be obtained using the union()
method or the pipe (|
) operator.
Here is an example of using the union()
method:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
union_set = set1.union(set2, set3)
print(union_set) # Output: {1, 2, 3, 4, 5}
Alternatively, we can use the pipe (|
) operator to achieve the same result:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
set3 = {3, 4, 5}
union_set = set1 | set2 | set3
print(union_set) # Output: {1, 2, 3, 4, 5}
The pop() Method
The pop()
method in Python's set data type removes and returns an arbitrary element from the set. If the set is empty, a KeyError
is raised.
Here is an example usage:
my_set = {1, 2, 3, 4}
popped_element = my_set.pop()
print(popped_element) # output: an arbitrary element from the set, e.g. 1
print(my_set) # output: the remaining elements in the set, e.g. {2, 3, 4}
Dive deep into the topic
Contribute with us!
Do not hesitate to contribute to Python tutorials on GitHub: create a fork, update content and issue a pull request.