Python Data Structures — Set
Prerequisites : Basic knowledge of any programming language
we will explore Data Structure Set and all its operations here.
Jupyter notebook for Set :
For all operation on List
For all operation on Dictionary
For all operation on Tuple
For common DataFrame
Here we will know about what is a set, how to create a Set , access elements in a Set, add element/s to the Set, remove an element from the Set. and we will find out if we can update elements is a Set.
What is a set?
- Set is unordered, and unindexed collection of data
- Elements in a set are immutable, cannot be changed
- Elements in a set must be unique
What is the use of a set?
- Removing duplicates from a sequence
- Computing standard mathematical operations like intersection, union, difference and symmetric difference.
- Set has a highly optimized method to check if an element exists in the set
Creating a set
A set can be created by placing values inside {}.
In the example below, we have “Tokyo” and “Mumbai”as duplicate values but since set can contain only unique values we can see that the output contains “Tokyo” and “Mumbai” only once
cities ={'Tokyo', 'Frankfurt','Mumbai', 'New York', 'Chicago', 'Tokyo', 'Mumbai'}
print(cities){'Frankfurt', 'Chicago', 'New York', 'Tokyo', 'Mumbai'}
we can also create a set using set() method. here you can see the elements are unordered
months =set(['Jan', 'Feb','March', 'April', 'May', 'June', 'July', 'Aug', 'Sep','Oct', 'Nov', 'Dec'])
print(months){'June', 'March', 'Dec', 'April', 'Jan', 'Aug', 'Sep', 'July', 'Nov', 'Oct', 'Feb', 'May'}
Accessing elements in a Set
since set is unordered collection of data, we cannot use index for access the elements in a set. We can use a for loop to access individual elements in a set
for i in months:
print(i)June
March
Dec
April
Jan
Aug
Sep
July
Nov
Oct
Feb
May
To check if an element is present in the set we can use the “in” operation.
In the below example, we check if Paris is present in the cities set, which returns False.
'Paris' in cities
Falsehere we check if Tokyo is present in cities, which returns True
'Tokyo' in cities
TrueAdding elements to a set
To add a single element to a set we can use add() method.
cities.add('Paris')
cities{'Chicago', 'Frankfurt', 'Mumbai', 'New York', 'Paris', 'Tokyo'}
If we want to add multiple elements to a set we need to use update()
cities.update(['Beijing','London','San Francisco'])
cities{'Beijing',
'Chicago',
'Frankfurt',
'London',
'Mumbai',
'New York',
'Paris',
'San Francisco',
'Tokyo'}
Change existing elements in a set
we cannot change elements in a set as set is immutable
Removing elements is a set
To remove an element we can use remove() method. If the element does not exists then remove will raise a “KeyError”
cities.remove('London')
cities{'Beijing',
'Chicago',
'Frankfurt',
'Mumbai',
'New York',
'Paris',
'San Francisco',
'Tokyo'}
we can also use discard() method to delete an element from the set. An error will not be raised if the element to be deleted does not exists in set.
cities.discard('Frankfurt')
cities{'Beijing', 'Chicago', 'Mumbai', 'New York', 'Paris', 'San Francisco', 'Tokyo'}
pop() method randomly removes an element from the set.
cities.pop()
cities{'Beijing', 'Chicago', 'Mumbai', 'New York', 'San Francisco', 'Tokyo'}
we can use clear() method to remove all the elements from the set
cities.clear()
citiesset()
Copying the elements from one set to another set
we can shallow copy elements from one set to another set. Shallow copy,when you change the content of one set and the other set remains unmodified.
Here we create cities and copy it’s content to copy_cities. we then add “Singapore” to the copt_cities set but see that cities set remains unmodified
cities ={'Tokyo', 'Frankfurt','Mumbai', 'New York', 'Chicago'}
copy_cities = cities.copy()
print(copy_cities)
{'Chicago', 'New York', 'Tokyo', 'Mumbai', 'Frankfurt'}copy_cities.add('Singapore')
print(copy_cities)
{'Singapore', 'Chicago', 'New York', 'Tokyo', 'Mumbai', 'Frankfurt'}print(cities)
{'Chicago', 'New York', 'Tokyo', 'Mumbai', 'Frankfurt'}
Finding number of elements in a set
we use len() method to find the number of elements in a set
len(copy_cities)6
Set Operations
In this section we will explore Union, Intersection, difference and symmetric difference
we create two sets, one multiples of 2 and one multiples of 3
set_muliple_2 ={2,4,6,8,10,12}
set_muliple_3 ={3,6,9,12,15}
Union will takes all elements from the two sets, so we will have all unique values which are either multiples of 2 or 3 or both

set_muliple_2.union(set_muliple_3)
{2, 3, 4, 6, 8, 9, 10, 12, 15}Intersection will take common elements between the two sets. In our example, we will have numbers that are both multiples of 2 and 3

set_muliple_2.intersection(set_muliple_3){6, 12}
Difference is performed using difference() method or “-”.
Difference will return element only present in the set_muliple_2 set but not in the present in set_muliple_3.
In other words it will returns all elements of set_muliple_2 and remove the common elements between set_muliple_2 and set_muliple_3

set_muliple_2.intersection(set_muliple_3)
{2, 4, 8, 10}set_muliple_2 - set_muliple_3
{2, 4, 8, 10}
Symmetric difference is performed using the “^” operator .
It will return unique elements between two sets but elements that are common between the two sets are excluded.
In our case, we will have all elements from the two sets and exclude the common elements between set_muliple_2 and set_muliple_3 which is 6 and 12

set_muliple_2 ^ set_muliple_3
{2, 3, 4, 8, 9, 10, 15}
