Python Sets
Python Sets
Sets
A set is an unordered collection of items.
Every set element is unique (no duplicates)
and must be immutable (cannot be changed).
However, a set itself is mutable. We can add
or remove items from it.
Sets can also be used to perform
mathematical set operations like union,
intersection, symmetric difference, etc.
Some more
definitions
Sets are used to store multiple items in a single
variable.
Set is one of 4 built-in data types in Python used to
store collections of data, the other 3 are List, Tuple,
and Dictionary, all with different qualities and
usage.
A set is a collection which
is unordered, unchangeable*, and unindexed.
* Note: Set items are unchangeable, but you can
remove items and add new items.
Sets are written with curly brackets.
Creating Python
Sets
A set is created by placing all the items
(elements) inside curly braces {}, separated
by comma, or by using the built-
in set() function.
It can have any number of items and they may
be of different types (integer, float, tuple,
string etc.). But a set cannot have mutable
elements like lists, sets or dictionaries as its
elements.
Example
# Different types of sets in Python
# set of integers
my_set = {1, 2, 3}
print(my_set)
Output
{1, 2, 3}
{1.0, (1, 2, 3), 'Hello'}
Set Items
Set items are unordered, unchangeable, and do not allow
duplicate values.
Unordered
Unordered means that the items in a set do not have a defined
order.
Set items can appear in a different order every time you use
them, and cannot be referred to by index or key.
Unchangeable
Set items are unchangeable, meaning that we cannot change the
items after the set has been created.
Once a set is created, you cannot change its items, but you can
remove items and add new items.
Duplicates Not Allowed
Sets cannot have two items with the same value.
Get the Length of a Set
To determine how many items a set has, use
the len() method.
Example:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
Output:
3
Set Items - Data Types
Set items can be of any data type: String, int
and boolean data types:
Example:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
A set can contain different data types:
A set with strings, integers and boolean values:
set1 = {"abc", 34, True, 40, "male"}
type()
From Python's perspective, sets are defined as objects with
the data type 'set': <class 'set'>
What is the data type of a set?
Example:
thisset = {"apple", "banana", "cherry"}
print(type(thisset))
Output:
<class 'set'>
The set() Constructor
It is also possible to use the set() constructor to make a set.
Using the set() constructor to make a set:
Example:
thisset = set(("apple", "banana", "cherry"))
# note the double round-brackets
print(thisset)
Output:
{'banana', 'cherry', 'apple'}
Modifying a set in
Python
add() – to add single element to the set
update() – to add multiple elements to the set
Note:
The update() method can take tuples, lists, strings
or other sets as its argument.
In all cases, duplicates are avoided.
# initialize my_set
my_set = {1, 3}
print(my_set)
# add an element
my_set.add(2)
print(my_set)
# add multiple elements
my_set.update([2, 3, 4])
print(my_set)
Output
{1, 3}
{1, 2, 3}
{1, 2, 3, 4}
{1, 2, 3, 4, 5, 6, 8}
Removing elements from
a set
discard() and remove()- to remove particular
element
print(my_set)
# remove an element
my_set.remove(6)
print(my_set))
# discard an element not present in my_set
my_set.discard(2)
print(my_set))
# remove an element not present in my_set you will get an error.
my_set.remove(2)
Output
{1, 3, 4, 5, 6}
{1, 3, 5, 6}
{1, 3, 5}
{1, 3, 5}
Traceback (most recent call last):
File "<string>", line 28, in <module>
KeyError: 2
Similarly, we can remove and return an item using the pop() method.
Since set is an unordered data type, there is no way of determining which item will be
popped. It is completely arbitrary.
We can also remove all the items from a set using the clear() method.
# initialize my_set
my_set = set("HelloWorld")
print(my_set)
# pop an element # Output: random element
print(my_set.pop())
# pop another element
my_set.pop()
print(my_set)
# clear my_set
my_set.clear()
print(my_set)
print(my_set)
Output
using | operator. Same can be accomplished
using the union() method.
Set Intersection-Intersection is performed
using & operator. Same can be accomplished
using the intersection() method.
Set Difference-Difference is performed
using - operator. Same can be accomplished
using the difference() method.
Set Symmetric Difference-Symmetric
difference is performed using ^ operator.
Same can be accomplished using the
method symmetric_difference().
Set Union Set Intersection
A = {1, 2, 3, 4, 5} A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8} B = {4, 5, 6, 7, 8}
print(A | B)
Output
print(A & B)
Output
{1, 2, 3, 4, 5, 6, 7, 8} {4, 5}
# use intersection
# use union function function on A
>>> A.union(B) >>> A.intersection(B)
{1, 2, 3, 4, 5, 6, 7, 8} {4, 5}
# use union function # use intersection
on B function on B
>>> B.union(A) >>> B.intersection(A)
{1, 2, 3, 4, 5, 6, 7, 8} {4, 5}
Set Difference Set Symmetric
A = {1, 2, 3, 4, 5} Difference
B = {4, 5, 6, 7, 8} A = {1, 2, 3, 4, 5}
print(A - B) B = {4, 5, 6, 7, 8}
Output
print(A ^ B)
{1, 2, 3}
Output
# use difference function {1, 2, 3, 6, 7, 8}
on A
>>> A.difference(B) # use symmetric_difference
{1, 2, 3} function on A
>>>
# use - operator on B
A.symmetric_difference(B) {1,
>>> B - A 2, 3, 6, 7, 8}
{8, 6, 7} # use symmetric_difference
# use difference function on B
function on B >>>
>>> B.difference(A) B.symmetric_difference(A) {1,
{8, 6, 7} 2, 3, 6, 7, 8}
Other Set
Operations
Set Membership Test
We can test if an item exists in a set or not, using the in keyword.
# in keyword in a set # initialize my_set
my_set = set("apple")
Output
True
False
Iterating Through a Set
We can iterate through each item in a set
using a for loop
Returns True if all elements of the set are true (or if the set is
all() empty).
Frozenset is a new class that has the characteristics of a set.
Frozensets are immutable sets.
Frozensets can be created using the frozenset() function.
This data type supports methods
like copy(), difference(), intersection(), isdisjoint(), issubset(),
issuperset(), symmetric_difference() and union().
Example: