A set
is an unordered collection with no duplicate elements, although mutable, and can have different types (integer, tuple, string etc). It cannot contain mutable elements such as lists
, or dictionaries
as its elements.
Sets can be used for mathematical set operations such as union, intersection, difference and symmetric difference.
This article is a continuation of Python Data Structures For Beginners
Let us explore set in more details... and in the end, solve a problem using set
data structure.
Topics covered in this post:
1. Creating Python Sets
2. Modifying a set in Python
3. Removing elements from a set
4. Set Union Operations
5. Set Intersection Operations
6. Set Difference Operations
7. Set Symmetric Difference
8. Python Set Methods
9. Set Code Challenge
# Different of set types in Python
# integers
set = {1, 2, 3}
# mixed data types
set = {3.2, "Hi", (1, 2, 3)}
# Distinguish set and dictionary while creating an empty set
# initialize a with {}
empty = {}
print(type(empty)) # Output => <class 'dict'>
# initialize a with set()
empty = set()
print(type(empty)) # Output => <class 'set'>
Sets are mutable and also unordered and therefore cannot be indexed, we cannot apply usual indexing or slicing when it comes to modifying set.
Single items are added with the add( ) method and multiple items added with the update( ) method. The update method can accept, list, tuple, dict. or string
set_example = {'hello', 'world'}
# you cannot call using indexing
# TypeError: 'set' object is not subscriptable
set_example[0]
# using add() method
# output => {'hello', 'yay!', 'world'}
set_example.add('yay!')
print(set_example)
# using update() method
# add multiple elements
# using different data types
# output => {1, 2, 'yay!', 3, 4, 5, 6, 7, 8, 'world', 'hello'}
set_example.update([1,2,3],{4,5,6},(7,8,))
print(set_example)
You can remove items from set using either discard( )
or remove( )
methods.
discard( )
will not raise an error if the item is not present in the set whilst remove( )
will raise an error.
set_example = {1, 3, 4, 5}
# discard an element
# output => {1, 4, 5}
set_example.discard(3)
print(set_example)
# discard element not present
# no errors raised
set_example.discard(30)
# remove an element not present
# KeyError: 30
set_example.remove(30)
We can also remove items using pop() and clear() methods.
Since set is unordered using pop() there is no way of knowing which element will be removed
set_example = {1, 3, 4, 5, 6}
# pop an element
# output => random element
set_example.pop()
print(set_example)
# pop another element
# output => random element
set_example.pop()
print(set_example)
# clear the set
# output => set()
set_example.clear()
print(set_example)
Union of two sets is a union that contains all elements of both sets. Union is performed with either |
operator or union( )
operator
# Set union
a = {1, 2, 3}
b = {4, 5, 6}
# using | operator
# output => {1, 2, 3, 4, 5, 6}
print(a | b)
# use union () function
# output => {1, 2, 3, 4, 5, 6}
print(a.union(b))
Intersection between two sets returns the elements the sets have in common.
It is performed using the &
operator or the intersection()
method
# Set Intersection
a = {1, 2, 3, 6, 7}
b = {4, 5, 6, 7, 8, 9}
# using & operator
# output => {6, 7}
print(a & b)
# use the intersection operator
# output => {6, 7}
print(a.intersection(b))
Set difference of (a - b) is a set with elements found in (a) but not in (b)
This is achieved either with the -
operator or the difference()
method.
# Set Difference
a = {1, 2, 3, 6, 7, 9}
b = {4, 5, 6, 7, 8, 9}
# using - operator
# output => {1, 2, 3}
print(a - b)
# using - operator
# output => {8, 4, 5}
print(b - a)
# using difference() function
# output => {1, 2, 3}
print(a.difference(b))
Symmetric difference of (a) and (b) are the elements found in (a) that are missing in (b) plus all elements found in (b) missing in (a)
This is achieved using ^
operator and the symmetric_difference()
# Set Symmetric Difference
a = {1, 2, 3, 6, 7, 9}
b = {4, 5, 6, 7, 8, 9}
# using ^ operator
# output => {1, 2, 3, 4, 5, 8}
print(a ^ b)
# using symmetric_difference method
# output => {1, 2, 3, 4, 5, 8}
print(a.symmetric_difference(b))
add()
- Adds an element to the setclear()
- Removes all the elements from the setcopy()
- Returns a copy of the setdifference()
- Returns a set containing the difference between setsdifference_update()
- Removes the items in this set that are also in anotherdiscard()
- Remove the specified itemintersection()
- Returns a set, that is the intersection of two other setsintersection_update()
- Removes items in a set that are not present in otherisdisjoint()
- Returns whether two sets have a intersection or notissubset()
- Returns whether another set contains this set or notissuperset()
- Returns whether this set contains another set or notpop()
- Removes an element from the setremove()
- Removes the specified elementsymmetric_difference()
- Returns a set with the symmetric differencessymmetric_difference_update()
- inserts the symmetric differences
union()
- Return a set containing the union of setsupdate()
- Update the set with the union of this set and others
Let's look at an example where the set
data structure could be used.
Determine if a word or phrase is an isogram.
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however, spaces and hyphens are allowed to appear multiple times.
Solution Steps
Time to see some code in action...
def is_isogram(string):
# return if alphabet letter
# make returned letter a lower case
# e.g: ['a','n','g','o','l','a']
string = [x.lower() for x in string if x.isalpha()]
# set will remove duplicates and return e.g ['n','g','o','l']
# len of set will not match len of original string
# and so will return False
return len(set(string)) == len(string)
# non repeating letters
is_isogram('lumberjacks') # Output => True
# repeating letters
is_isogram('Angola') # Output => False
Conclusion
Facts about the set
data structure it is unordered and mutable but yet can only contain immutable types, it cannot contain duplicates making it ready handy when we have to work on a situation where uniqueness is important.
Now when a problem you are trying to solve meets any of the above facts about set()
then you know how to go about tackling it.