Motivation
Motivation
ipynb - Colab
keyboard_arrow_down Sets
Sets are an essential data structure in programming that allow you to store a collection of unique
elements. A set is an unordered collection, which means that the elements are not stored in any
particular order. The primary purpose of sets is to group related elements together and perform
various operations on them.
Motivation
Welcome to your journey of learning about sets in programming! Let's explore the key reasons
why learning about sets is valuable:
Uniqueness and Deduplication: Sets ensure that each element appears only once, making
them valuable for tasks such as removing duplicates from a collection or finding unique
elements in data. They help you work with distinct and non-repetitive data, which is
important for many programming tasks.
Efficient Membership Testing: Sets are super fast when it comes to checking if something
is part of the group or not, which makes them perfect when you need to quickly find out if
something is already in a group or not.
Mathematical Set Operations: Sets support various mathematical operations. You can
combine sets, compare them, or even take away some things from one set to make a new
one.
Characteristics of Sets
Unique Elements: A set does not allow duplicate elements. Each element in a set is unique,
meaning that it appears only once. If you attempt to add a duplicate element to a set, it will
be ignored.
Unordered Nature: The elements in a set are not stored in a specific order. Unlike lists or
tuples, where the order of elements is significant, sets are not concerned with the
sequence of elements. This characteristic allows sets to be efficient in performing
operations like membership checks and eliminating duplicates.
In Python, you can create sets using curly braces {} or the set() constructor.
Observe that in the above example that number 4 appears only once in the set, as an unique
entry.
my_set = {1, 2, 3, 4, 4, 4, 6}
print(my_set)
It's important to note that using curly braces {} without any elements will create
an empty dictionary instead of an empty set. To create an empty set, always use
the set() constructor.
Trying to index the set will result in a TypeError as elements don't have a specific
order in a set, as mentioned above.
my_set[1]
fruits.add("apple")
print(fruits)
To add multiple elements to a set, use the update() method. For example:
fruits.update(["banana", "orange"])
print(fruits)
print(fruits)
fruits.remove("banana")
print(fruits)
To remove and return an arbitrary element from a set, use the pop() method. For example:
print(fruits)
popped_element = fruits.pop()
print(popped_element)
print(fruits)
print("apple" in fruits)
print("apple" not in fruits)
my_set = {1, 5, 3, 6, 7, 5, 4, 5, 5, 5, 6}
len(my_set)
print(min(my_set))
print(max(my_set))
Sets in Python share the same principles as sets in mathematics and can support the same
operations. The most common ones are Union , Intersection , Difference , and Symmetric
Difference .
keyboard_arrow_down .union()
The union of two sets is a new set that contains all the unique elements from both sets. In
Python, you can find the union of sets using the union() method or the | operator. For example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)
# Output: {1, 2, 3, 4, 5}
keyboard_arrow_down .intersection()
The intersection of two sets is a new set that contains the common elements present in both
sets. In Python, you can find the intersection of sets using the intersection() method or the &
operator. For example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
# Output: {3}
keyboard_arrow_down .difference()
The difference between two sets is a new set that contains the elements present in the first set
but not in the second set. In Python, you can find the difference between sets using the
difference() method or the - operator. For example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
print(difference_set)
# Output: {1, 2}
keyboard_arrow_down .symmetric_difference()
The symmetric difference between two sets is a new set that contains the elements that are
present in either of the sets but not in both. In Python, you can find the symmetric difference
between sets using the symmetric_difference() method or the ^ operator. For example:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set)
# Output: {1, 2, 4, 5}
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
set1_subset_of_set2 = set1.issubset(set2)
print(set1_subset_of_set2)
# Output: True
set2_superset_of_set1 = set2.issuperset(set1)
print(set2_superset_of_set1)
# Output: True
set1 = {1, 2, 3}
set2 = {4, 5, 6}
are_disjoint = set1.isdisjoint(set2)
print(are_disjoint)
# Output: True
Key Takeaways