Python Sets
• 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.
• Sets are written with curly brackets separated by comma.
• It is an unordered sequence of unique elements where elements may be of different
immutable type objects such as integer, float, string, tuple.
• Set is a mutable data type like list that can be modified.
Declaration
Set1=set() # empty set
Set2={‘c’, ‘java’, ‘python’) # set of strings
Set3={10, 20, 30, 40} # set of integers
Set4={“Apple”, “Orange”, 100}
Set5=set([10, 20, 30, 20, 30, 40]) # creating set after removing duplicate elements
Mutability of Sets : update() method
Set is a mutable data type and can be modified. We can add elements of a set to another set
provided that all duplicate elements are avoided, using update() method.
Set1={10, 20, 30, 40}
Set2={60, 70}
Set1.update(Set2)
Advantages of Sets over Lists & Tuples
• List and Tuples may have duplicate elements, but sets cannot
Creating sets using list, tuple and string
List1=[10, 20, 40, 55]
Tuple1=(‘C’, ‘C++’, ‘Java’, (3, 4))
String1=”PythonProgram”
Set1=set(List1)
Set2=set(Tuple1)
Set3=set(String1)
Frozen Sets
A frozen set is a set that cannot be changed once it is created. Unlike sets, frozen sets are
immutable. Frozen sets are created using the function frozenset().
set1 = {10, 20, 30, 40, 50}
list1 = [10, 20, 20, 30, 40]
fset1 = frozenset() #empty frozen set
fset2 = frozenset(set1)
fset3 = frozenset(list1)
Set Operations
There are many set operations like intersection, union, difference, etc. Sets have various functions
and methods for these operations.
add() Method
The add() method can be used to insert an element to a set, provided that all duplicates are avoided.
Set1 = {10, 20, 30, 40}
Set1.add(60)
Print(Set1)
remove() Method
The remove() method can be used to delete an element from a set. If the required element is not
present in the set, it returns an error message.
Set1.remove(30)
union() Method
The union operation returns a set having elements from both sets provided that all duplicates are
avoided.
a = {10, 20, 30, 40, 50}
b = {40, 50, 60, 70}
print(a.union(b))
intersection() Method
The intersection operation returns a set having common elements from two sets.
print(a.intersection(b))
Set functions & methods
Function / Method Description Example
Removes elements from set
and does not return error if s={1, 2, 3, 4, 5}
discard()
element is not present in the s.discard(3)
set.
Removes and returns any
pop() print(s.pop())
arbitrary element from set.
Removes all elements from
clear() s.clear()
set.
s={1, 2, 3, 4, 5}
Returns true if every element
t={1,3,4}
s.issubset(t) in in set s is present in set t,
print(s.issubset(t))
false otherwise.
OUTPUT: False
s={1, 2, 3, 4, 5}
Returns true if every element
t={1,3,4}
s.isupperset(t) in in set t is present in set s,
print(s.isupperset(t))
false otherwise.
OUTPUT: True
Set1={10, 20, 30, 40, 50}
Returns set s having elements Set2={10, 30, 40, 60}
s.intersection_update(t) which are common to both Set1.intersection_update(Set2)
set s and t. print(Set1)
OUTPUT: {40, 10, 30}
Set1={10, 20, 30, 40, 50}
Returns a set that has
s.difference(t) Set2={10, 30, 40, 60}
elements in set s but not in
or s-t print(Set1.difference(Set2))
set t
OUTPUT: {50, 20}
Set1={10, 20, 30, 40, 50}
Set2={10, 30, 40, 60}
s.difference_update(t) Returns set s after removing
Set1.difference_update(Set2)
elements which are in set t.
print(Set1)
OUTPUT: {50, 20}
Set1={10, 20, 30, 40, 50}
Returns a set with elements
s.symmetric_difference(t) Set2={10, 30, 40, 60}
either in set s or in set t but
or s^t Print(Set1.symmetric_difference(Set2))
not both.
OUTPUT: {50, 20, 60}
Set1={10, 20, 30, 40, 50}
Returns true if set s and t have Set2={10, 30, 40, 60}
s.isdisjoint(t)
null intersection. Print(Set1.isdisjoint(Set2))
OUTPUT: False
Set1={10, 20, 30, 40, 50}
len(s) Returns length of the set s Print(len(Set1))
OUTPUT: 5
Set1={10, 20, 30, 40, 50}
s.copy() Returns a copy of set s Set2=Set1.copy()
print(Set2)
Returns maximum valued
max(s) print(max(Set1))
element from set s
Returns minimum valued
min(s) print(min(Set1))
element from set s
Returns sum of elements in
sum(s) print(sum(Set1))
set s
Returns a sorted list of
sorted(s) print(sorted(Set1))
elements from set s