Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Python Set

Python Set

May 20th, 2019

Python Sets

Sets are an unordered collection of unique elements. We can construct them by using the set() function. Let’s go ahead and make a set to see how it works.

Set and Booleans

There are two other object types in Python that we should quickly cover:

  • Sets
  • Booleans

Creating a Set

Sets can be created with set constructor or by directly defining the values in a {} brackets

a1 = set(),#list()
b2 = {1,2,3}
print(type(a1)
print(type(b2))
<class 'tuple'>
<class 'set'>

Various methods in Set

  • membership “in”
  • isdisjoint
  • subset
  • propersubset
  • superset
  • proper superset
  • union
  • intersection
  • difference
  • intersection_update
  • difference_update
  • symmetric_difference_update
  • copy
  • add
  • pop
  • remove
  • discard
  • clear

 

1. membership “in”

Example

a = {1,2,3,4,5} #a=1,2 #print(type(a)) b= {4,5,6}

c ={6,7,8}

d= {6,7,9,10,11 }

e={0,1,6,7,11,22,33}

f= {1,2,3,6,7,8}

ab ={1,2, 3, 4, 5}

aa= {“ab”,”bc”,”de”} z =4

#print(len(a)) print(z in b) print(“######################”) #checking conditions

print(“checking membership of c in ab code: c not in ab : result ={0}”.format(z not in ab)) print(“######################”)

True

######################

checking membership of c in ab code: c not in ab : result =False

######################

2.Isdisjoint

Example

print(“checking if set a and ab are disjoint a is disjoint to ab code:

a.isdisjoint(ab):result: {0} “.format(a.isdisjoint(ab)))

print(“######################”)

checking if set a and ab are disjoint a is disjoint to ab code: a.isdisjoint(ab): result: False

######################

3. subset

Example

print(“check if all the members of a are members of set ab code:-
a.issubset(ab): result – {0}”.format(a.issubset(ab)))
print(“######################”)
print(“check if all the members of a are members of set ab, , using symbol
code: a <= ab : result- {0}”.format(a <= ab))
print(“check if all the members of a are members of set ab and not equals to set ab ie proper subset
code: a < ab : result – {0}”.format(a < ab))
print(“######################”)
check if all the members of a are members of set ab
code:- a.issubset(ab): result – True
######################
check if all the members of a are members of set ab, , using symbol code:
a <= ab : result – True
check if all the members of a are members of set ab and not equals to set ab ie proper subset
code: a < ab : result – False

################

4. propersubset

print(“check if all the members of a are members of set ab, here ab is called as superset of a code:
ab.issuperset(a) : result – {0} “.format(ab.issuperset(a)))
print(“check if all the members of a are members of set ab, here ab is called as superset of a, using symbol
code: ab >= a result: – {0}”.format(ab >= a))
print(“######################”)
print(“check if all the members of a are members of set ab, here ab is called as superset of a, and is a proper superset code: ab > a result: – {0}”.format(ab > a))

5.Union

Example

print(“Union or combining elements from two sets code: a.union(b) : result: {}”.format(a.union(b)))

print(“Union or combining elements from two sets – using symbol code: a|b

:result: {}”.format(a|b)) print(“######################”)

6.Intersection

Example

print(“finding the common items between two sets code: a.intersection(b)

result: – {0}”.format(a.intersection(b)))

print(“finding the common items between two sets -using symbol code: a & b

result: – {0}”.format(a&b)) print(“######################”)

7.Difference

Example

print(“find the difference between two sets code: a.difference(b) {0}”.format(a.difference(b)))

print(“find the difference between two sets, a -b code: a-b : result: – {0} “.format(a-b))

#print(“udpating one set with elements in other set code: c.update(b) – result {0}”.format(c.update(b))) print(“######################”)

#c|=b

print(“C set is {0} “.format(c)) print(“D set is {0} “.format(d))

print(“intersection update between c and d: code: d.intersection_update(c)

:result – {0}”.format(d.intersection_update(c))) #c &= d

print(“C set is {0} “.format(c)) print(“D set is {0} “.format(d))

find the difference between two sets code: a.difference(b) {1, 2, 3}

find the difference between two sets, a -b code: a-b : result: – {1, 2, 3}

######################

C set is {8, 6, 7} D set is {6, 7}

intersection update between c and d: code: d.intersection_update(c) : result – None

C set is {8, 6, 7} D set is {6, 7}

8. Difference_update

Example

print(“C set value is {0} “.format(c)) print(“E set value is {0} “.format(e))

print(“difference updated between e and c code: e.difference_update(c) : result – {0}”.format(e.difference_update(c)))

print(“E set becomes {0} “.format(e)) print(“######################”)

C set value is {8, 6, 7}

E set value is {0, 1, 33, 6, 7, 11, 22}

difference updated between e and c code: e.difference_update(c) : result – None

E set becomes {0, 1, 33, 11, 22}

9. intersection_update

Example

print(“D set becomes {0} “.format(d)) print(“######################”)

#

#print(“intersection update between c and d: code: d.intersection_update(c)

:result – {0}”.format(d.intersection_update(c)))

# print(“D set becomes {0} “.format(d))

#

#

print(“B set value is {0} “.format(b)) print(“C set value is {0} “.format(c)) print(“D set value is {0} “.format(d))

print(“difference updated between b and d , updates the code: b.difference_update(d) : result – {0}”.format(b.difference_update(d))) print(“B set becomes {0} “.format(b))

#print(“difference updated between b and c , updates the using the symbol: b -= c : result – {0}”.format(b -= c))

print(“B set becomes {0} “.format(b)) print(“######################”)

D set becomes {6, 7}

######################

B set value is {4, 5, 6} C set value is {8, 6, 7} D set value is {6, 7}

difference updated between b and d , updates the code: b.difference_update(d) : result – None

B set becomes {4, 5} B set becomes {4, 5}

################

10. symmetric_difference_update

Example

print(“F set becomes {0} after symmetric difference”.format(f))
print(“A set becomes {0} after symmetric difference”.format(a))
print(“symmetric difference updated between a and f code: a.symmetric_difference_update(f) : result – {0}”.format(a.symmetric_difference_update(f))) print(“A set becomes {0} “.format(a))
print(“doing the operation again”)
a ^= f
print(“F set becomes {0} after symmetric difference”.format(f))
print(“A set becomes {0} after symmetric difference”.format(a)) F set becomes {1, 2, 3, 6, 7, 8} after symmetric difference
A set becomes {1, 2, 3, 4, 5} after symmetric difference symmetric difference updated between a and f code: a.symmetric_difference_update(f) : result – None
A set becomes {4, 5, 6, 7, 8}
doing the operation again
F set becomes {1, 2, 3, 6, 7, 8} after symmetric difference A set becomes {1, 2, 3, 4, 5} after symmetric difference

11.Copy

Example

print(aa)
z= aa.copy()
print(“copying to another variable z {0}”.format(z))
{‘bc’, ‘ab’, ‘de’}
copying to another variable z {‘bc’, ‘ab’, ‘de’}

12. Add

Example

aa.add(“xy”)
print(“add condition result {0}”.format(aa))
aa.add(“xy1”)
print(“add condition result {0}”.format(aa))
aa.add(“ay”)
print(“add condition result {0}”.format(aa))
aa.add(“ay1”)
#aa.sort()
print(“add condition result {0}”.format(aa))
aa.add(“abc”)
print(“add condition result {0}”.format(aa)) add condition result {‘bc’, ‘ab’, ‘xy’, ‘de’}
add condition result {‘bc’, ‘de’, ‘xy1’, ‘xy’, ‘ab’}
add condition result {‘bc’, ‘de’, ‘xy1’, ‘xy’, ‘ab’, ‘ay’}
add condition result {‘bc’, ‘de’, ‘xy1’, ‘xy’, ‘ab’, ‘ay’, ‘ay1’}
add condition result {‘bc’, ‘de’, ‘xy1’, ‘abc’, ‘xy’, ‘ab’, ‘ay’, ‘ay1’}

13. pop

Example

aa.pop()
print(“pop condition result {0}”.format(aa))
pop condition result {‘de’, ‘xy1’, ‘abc’, ‘xy’, ‘ab’, ‘ay’, ‘ay1’}

14. Remove

Example

aa.remove(“ab”)
print(“remove condition result {0}”.format(aa))
KeyError  Traceback (most recent call last)
<ipython-input-150-16d506463ffa> in <module>()
aa.remove(“ab”)
print(“remove condition result {0}”.format(aa))

KeyError: ‘ab’

15. Discard

Example

aa.discard(“abc”)
print(“discard condition result {0}”.format(aa))
discard condition result {‘de’, ‘xy1’, ‘xy’, ‘ay’, ‘ay1’}

16. Clear

Example

In [153]:
aa.clear()
print(“clear condition result {0}”.format(aa))
clear condition result set()