SETS IN PYTHON

Sets

  • Sets are similar to lists but with a difference that sets are lists with no duplicate entries.
  • set is a mutable and an unordered collection of items. Means we can easily add or remove items from it.
  • A set is created by placing all the elements inside curly brackets { }, seperated by comma or by using the built-in function set().
Syntax: set_variable = {val1, val2, ... }
Example: 
1.S={1,2.0.”abc”} 
2.set([1,2.0,”abc”])  

A set can have any number of items and they may be of different data types.

SET OPERATIONS

 Note:

  • 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 and only if the first set is a subset of the second set.
  • A set is greater than another set if and only if the first set is a superset of the second set.
S.NOOPERATIONUSAGEEXAMPLE
1updateAdds elements of one set to the other sets provided that all duplicates are avoided.
Syntax: set1.update(set2)
s=set([1,2,3,4,5])
t=set([6,7,8])
s.update(t)
print(s) #prints {1, 2,3,4, 5, 6, 7, 8}
2addAdds an element to the set provided that all duplicates are avoided
Syntax:set.add(element)
s=set([1,2,3,4,5])
s.add(6)
print(s)#prints {1, 2, 3, 4, 5, 6}
3removeRemoves an element from the set. Returns KeyError if the element is not present
Syntax:set.remove(element)
s=set([1,2,3,4,5])
s.remove(2)
print(s)#prints {1, 3, 4, 5}
4discardSame as remove() but does not give an error if the element is not present in the set
Syntax:set.discard(element)
s=set([1,2,3,4,5])
s.discard(2)
print(s)#prints {1, 3, 4, 5}
5popRemoves and returns an arbitrary element from a set.KeyError is raised if a set is
empty.
Syntax:set.pop()
s=set([1,2,3,4,5])
s.pop()#returns 1
print(s)#prints {2, 3, 4, 5}
6clearRemoves all elements from the set.
Syntax:set.clear()
s=set([1,2,3,4,5])
s.clear()
print(s)#prints set()
7lenReturns the length of the set
Syntax: len(set)
s=set([1,2,3,4,5])
p=len(s)
print(p)#prints 5
8inReturns True if the element is present in set and False otherwise
Syntax: element in set
s=set([1,2,3,4,5])
p=3 in s
print(p) #prints True
9not in Returns True if the element is not present in set and False otherwise
Syntax: element not in set
s=set([1,2,3,4,5])
p=6 not in s
print(p) #prints True
10issubsetReturns True if every element in set s is present in set t and False otherwise
Syntax: s.issubset(t) or s<=t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.issubset(p)) #prints True
print(s<=p) #prints True
11issupersetReturns True if every element in t is present in set t and False otherwise
Syntax: s.issuperset(t) or s>=t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.issuperset(p)) #prints False
print(s>=p) #prints False
12unionReturns a set s that has elements from both sets s and t
Syntax: s.union(t) or s|t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.union(p))#prints {1, 2, 3, 4, 5, 6, 7,8}
print(s|p)#prints {1, 2, 3, 4, 5, 6, 7, 8}
13intersectionReturns a new set that has elements that are common to both sets s and t
Syntax: s.intersection(t) or s&t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.intersection(p))#prints {1, 2, 3, 4, 5}
print(s&p)#prints {1, 2, 3, 4, 5}
14intersection_updateReturns a set that has elements that are common to both the sets s
and t
Syntax: s.intersection_update(t)
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
s.intersection_update(t)
print(s)#prints {1, 2, 3, 4, 5, 6, 7,8}
15differenceReturns a new set that has elements in set s but not in t.
Syntax: s.difference(t) or s-t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.difference(p)) #prints set()
print(s-t)#prints set()
16difference_updateRemoves all elements of another set from this set
Syntax: s.difference_update(t)
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
s.difference_update(p)
print(s) #prints set()
17symmetric differenceReturns a new set with elements either in s or in t but not both.
Syntax: s.symmentric_difference(t) ors^t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.symmetric_difference(p))#prints {6, 7,8}
print(s^t)#prints {6, 7,8}
18copyReturns a copy of set s
Syntax: set.copy()
s=set([1,2,3,4,5])
p=s.copy()
print(p) #prints {1, 2, 3, 4, 5}
19isdisjoint Returns True if two sets have a null intersection.
Syntax: s.isdisjoint(t)
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s.isdisjoint(p)) #prints False
20equals(==)returns True if the two set are equivalent and False otherwise
Syntax:s==t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s==p) #prints False
21Not equals(!=)returns True if both sets are not equivalent and False otherwise
Syntax: s!=t
s=set([1,2,3,4,5])
p=set([1,2,3,4,5,6,7,8])
print(s!=p) #prints True
22allReturns True if all elements in the set are True otherwise False.
Syntax: all(s)
s=set([1,2,3,4,5])
print(all(s)) #prints True
23anyReturns True if any of the elements in the set is True.
Returns False if the set is empty.
Syntax: any(s)
s=set([1,2,3,4,5])
print(all(s)) #prints True
24enumerateReturns an enumerate object which contains the index as well as the value of all the items of the set as a pair.
Syntax:enumerate(s)
s=set([1,2,3,4,5])
for i in enumerate(s):
print(i,end=’ ‘)
prints (0, 1) (1, 2) (2, 3) (3, 4) (4, 5)
25maxReturns the maximum value in a set
Syntax: max(set)
s=set([1,2,3,4,5])
print(max(s))#prints 5
26minReturns the minimum value in a set
Syntax: min(set)
s=set([1,2,3,4,5])
print(min(s))#prints 1
27sumReturns the sum of elements in a set
Syntax: sum(s)
s=set([1,2,3,4,5])
print(sum(s))#prints 15
28sortedReturns a new sorted list from elements in the set. It does not sort the set as set are immutable.
Syntax:sorted(s)
s=set([5,4,3,2,1])
print(sorted(s))#prints {1,2,3,4,5}

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?