Python Programming
Language
Sets
• Sets - another data structure supported by Python.
• Sets are similar to lists but with a difference that sets are lists with no duplicate entries.
• A set is a mutable and an unordered collection of items. Means we can easily add or
remove items from it.
• A set can have any number of items and they may be of different data types.
• The major advantage of using a set, as opposed to a list, is that it has a highly optimized
method for checking whether a specific element is contained in the set.
• Syntax: set_variable = {val1, val2, ... }
• s={1,2.0,"abc"}
• s={1,2,3,1,2}
• p=set([1,2.0."abc"])
Set Operations
• Two sets are equal if and only if every element in each set is contained in the other.
• A set is less than another set if an only if the first set is a subset of the second set.
>>> s={2,3,4}
>>> s1={5,2,3,4,6}
>>> print(s<s1)
True
• A set is greater than another set if an only if the first set is a superset of the second set.
>>> print(s1>s)
True
Set Properties
• A set cannot contain other mutable objects (like lists).
S={10,20,[30,40]} is invalid.
• To make an empty set we have to use set() but not s={ } , this creates empty dictionary.
• Since sets are unordered, indexing have no meaning.
• Set operations do not allow access or change an element using indexing or slicing.
p={1,2,3}
p[0]=4 #error
• But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.
set1 = set(["Vasavi", "College", "of", "Engineering"])
print("\nInitial set")
print(set1)
print("\nElements of set: ")
for i in set1:
print(i, end=" ")
print("Vasavi" in set1)
Set Operations
Operation Description Code Output
s.update(set t) Adds elements of set t in the s=set([1,2,3,4,5]) {1,2,3,4,5,6,7,8}
set s provided that all t=set([6,7,8])
duplicates are avoided. s.update(t)
print(s)
s.add(x) Adds element x to the set s s=set([1,2,3,4,5]) {1,2,3,4,5,6}
provided that all duplicates s.add(6)
are avoided print(s)
s.remove(x) Removes element x from set s=set([1,2,3,4,5]) {1,2,4,5}
s. Returns KeyError if x is s.remove(3)
not present print(s)
s.discard(x) Same as remove() but does s=set([1,2,3,4,5]) {1,2,4,5}
not give an error if x is not s.discard(3)
present in the set print(s)
s.pop() Removes and returns any s=set([1,2,3,4,5]) {2,3,4,5}
arbitrary element from s. s.pop()
KeyError is raised if s is print(s)
empty.
Set Operations
Operation Description Code Output
s.clear() Removes all elements from s=set([1,2,3,4,5]) set()
the set. s.clear()
print(s)
len(s) Returns the length of set s=set([1,2,3,4,5]) 5
print(len(s))
x in s Returns True if x is present s=set([1,2,3,4,5]) True
in set s and False otherwise print(3 in s)
x not in s Returns True if x is not s=set([1,2,3,4,5]) False
present in set s and False print(3 not in s)
otherwise
s.issubset(t) or Returns True if every s=set([1,2,3,4,5]) True
s<=t element in set s is present t=set([1,2,3,4,5,6,7,8,9,1
in set t and False otherwise 0])
print(s<=t)
s.issuperset(t) Returns True if every s=set([1,2,3,4,5]) False
or s>=t element in t is present in t=set([1,2,3,4,5,6,7,8,9,1
set t and False otherwise 0])
print(s>=t)
Set Operations
Operation Description Code Output
s.union(t) or s|t Returns a new set that has s=set([1,2,3,4,5]) {1,2,3,4,5,6,
elements from both sets s T=set([1,2,3,4,5,6,7,8,9,10 7,8,9,10}
and t ])
print(s|t)
s.intersection(t) Returns a new set that has s=set([1,2,3,4,5]) {1,2,3,4,5}
or s&t elements which are common t=set([1,2,3,4,5,6,7,8,9,10]
to both sets s and t )
z=s&t
print(z)
s.intersection_u Returns a set that has s=set([1,2,10,12]) {1,2,10}
pdate(t) elements which are common t=set([1,2,3,4,5,6,7,8,9,10]
to both the sets s and t )
s.intersection_update(t)
print(s)
s.difference(t) Returns a new set that has s=set([1,2,10,12]) {12}
or s-t elements in set s but not in t. t=set([1,2,3,4,5,6,7,8,9,10]
)
z=s-t
print(z)
Set Operations
Operation Description Code Output
all(s) Returns True if all elements in s=set([0,1,2,3]) False
the set are True otherwise t=set([4,5,6])
False. print(all(s))
any(s) Returns True if any of the s=set([0,1,2,10,12]) True
elements in the set is True. print(any(s))
Returns False if the set is
empty.
enumerate(s Returns an enumerate object s=set(['a','b','c','d']) (0,’a’)(1.’b’)(2,’c’)(3,’d’)
) which contains index as well for i in enumerate(s):
as value of all the items of set print(i,end=" ")
as a pair.
max(s) Returns the maximum value s=set([1,2,3])
min(s) in a set print(max(s)) 3
sum(s) Returns the minimum value in print(min(s)) 1
a set print(sum(s)) 6
Returns the sum of elements
in a set
sorted(s) Returns a new sorted list from s=set(["hello","world {'world', 'welcome',
elements in the set. It does not ","hi","welcome"]) 'hello', 'hi’}
sorts the set. print(s) ['hello', 'hi', 'welcome',
print(sorted(s)) 'world']