Set Built In Methods

 

Built in Methods for SET Class to perform certain operations

  • add() : add an element to a SET


#program

s1 = {10, 20, 30, 40, 50, 40, 30, 20, 10} # provided repeat items
print("s1 : ", s1)

s1.add(
60)
print("s1.add(60) : ", s1)

#output

s1 : {40, 10, 50, 20, 30}
s1.add(
60) : {40, 10, 50, 20, 60, 30}


  • clear() : removes all elements from the SET

#program

s1 = {10, 20, 30, 40, 50, 40, 30, 20, 10} # provided repeat items
print("s1 : ", s1)

s1.clear()
print("s1.clear() : ", s1)

#output

s1 : {40, 10, 50, 20, 30}
s1.clear() :
set()


  • copy() : return a shallow copy of the SET

#program

s1 = {10, 20, 30, 40, 50, 40, 30, 20, 10} # provided repeat items
print("s1 : ", s1)

s2 = s1.copy()
print("s2 : ", s2)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
50, 20, 40, 10, 30}


  • difference() : return the difference of 2 or more SET as a new SET

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.difference(s2)
print("s3 : ", s3)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 : {
40, 20}



#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
60, 70, 80}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.difference(s2)
# returns items which are different in s1 w.r.t. items in s2
s4 = s2.difference(s1) # returns items which are different in s2 w.r.t. items in s1
print("s3 : ", s3)
print("s4 : ", s4)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
80, 60, 70}
s3 : {
40, 10, 50, 20, 30}
s4 : {
80, 60, 70}


  • difference_update() : remove all items of other set from current SET

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
s3 = {
60, 70, 80}
print("s1 : ", s1)
print("s2 : ", s2)
print("s3 : ", s3)

s1.difference_update(s2)
# remove s2 items from s1
print("s1.difference_update(s2) : ", s1)
s2.difference_update(s3)
# remove s3 items from s2
print("s2.difference_update(s3) : ", s2)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 : {
80, 60, 70}
s1.difference_update(s2) : {
40, 20}
s2.difference_update(s3) : {
10, 50, 30}    # no common item in s2 and s3, hence no change in s2


  • discard() : remove the specified item

#program

s1 = {10, 20, 30, 40, 50,}
print("s1 : ", s1)

s1.discard(
40)
print(" s1.discard(40) : ", s1)

#output

s1 : {40, 10, 50, 20, 30}
s1.discard(
40) : {10, 50, 20, 30}


  • intersection() : return the intersection of 2 SET as a new SET

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
s3 = {
60, 90}
print("s1 : ", s1)
print("s2 : ", s2)
print("s3 : ", s3)

s4 = s1.intersection(s2)
# s2 have some items as s1
print("s4 = s1.intersection(s2) : ", s4)

s4 = s1.intersection(s3)
# s3 items are not in s1
print("s4 = s1.intersection(s3) : ", s4)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 : {
90, 60}
s4 = s1.intersection(s2) : {
10, 50, 30}
s4 = s1.intersection(s3) :
set()


  • isdisjoint() : return True whether 2 SET  do not have intersection or not

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
s3 = {
60, 90}
print("s1 : ", s1)
print("s2 : ", s2)
print("s3 : ", s3)

s4 = s1.isdisjoint(s2)
# s2 have some items as s1
print("s4 = s1.isdisjoint(s2) : ", s4)

s4 = s1.isdisjoint(s3)
# s3 items are not in s1
print("s4 = s1.isdisjoint(s3) : ", s4)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 : {
90, 60}
s4 = s1.isdisjoint(s2) :
False
s4 = s1.isdisjoint(s3) : True


  • issubset() : return True if passed SET is having calling SET

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.issubset(s2)             # s2 have some items as s1
print("s3 = s1.issubset(s2) : ", s3)

s3 = s2.issubset(s1)
            # s3 items are not in s1
print("s3 = s2.issubset(s1) : ", s3)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 = s1.issubset(s2) :
False     # s1 is not subset of s2
s3 = s2.issubset(s1) : True      # s2 is subset of s1



  • issuperset() : return True if calling SET is having passed SET items

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.issuperset(s2)             # s2 have some items as s1
print("s3 = s1.issuperset(s2) : ", s3)

s3 = s2.issuperset(s1)
            # s3 items are not in s1
print("s3 = s2.issuperset(s1) : ", s3)

#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 50, 30}
s3 = s1.issuperset(s2) :
True        # s1 is superset of s2
s3 = s2.issuperset(s1) : False      # s2 is not superset of s1


  • pop() : remove and return arbitrary set element, raise key error if SET is empty .

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30, 50}
s3 = {}

print("s1 : ", s1)
print("s2 : ", s2)

s4 = s1.pop()
print("s4 : ", s4)

s4 = s2.pop()
print("s4 : ", s4)

s3.clear()
s4 = s3.pop()
print("s4 : ", s4)



#output

s1 : {40, 10, 50, 20, 30} s2 : {10, 50, 30} s3 : {} s4 : 40 s4 : 10

Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 17, in <module>
s4 = s3.pop()
TypeError: pop expected at least 1 argument, got 0



  • remove() : remove an element from SET, raise key error if element is not a member.

#program

s1 = {10, 20, 30, 40, 50,}
print("s1 : ", s1)

s1.remove(
30)                    #removing 30
print("s1.remove(30) : ", s1)

s1.remove(
30)                    # removing 30 again
print("s1.remove(30) : ", s1)


#output

s1 : {40, 10, 50, 20, 30}
s1.remove(
30) : {40, 10, 50, 20}
Traceback (most recent call last):
File "
C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 9, in <module>
s1.remove(30)
KeyError: 30



  • union() : return a union of set in a new set.

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
60, 70}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.union(s2)
print("s3 : ", s3)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
60, 70}
s3 : {
50, 20, 70, 40, 10, 60, 30}


  • update() : update the set with union of itself and others.

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
60, 70}
print("s1 : ", s1)
print("s2 : ", s2)

s1.update(s2)
print("s1 : ", s1)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
60, 70}
s1 : {
70, 40, 10, 50, 20, 60, 30}


  • symmetric_difference() : returns a set with a symmetric difference of 2 SET.

#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
10, 30}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.symmetric_difference(s2) # s2 and s1 have few common members
print("s3 : ", s3)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 30}
s3 : {
40, 50, 20}



#program

s1 = {10, 20, 30, 40, 50,}
s2 = {
60, 70}
print("s1 : ", s1)
print("s2 : ", s2)

s3 = s1.symmetric_difference(s2)
print("s3 : ", s3)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
60, 70}
s3 : {
70, 40, 10, 50, 20, 60, 30}


  • symmetric_difference_update() : insert the symmetric difference from this SET to other SET.

#program

s1 = {10, 20, 30, 40, 50}
s2 = {
10, 30}
print("s1 : ", s1)
print("s2 : ", s2)

s1.symmetric_difference_update(s2)
print("s1 : ", s1)
print("s2 : ", s2)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 30}
s1 : {
40, 50, 20}
s2 : {
10, 30}



#program

s1 = {10, 20, 30, 40, 50}
s2 = {
10, 30}
print("s1 : ", s1)
print("s2 : ", s2)

s2.symmetric_difference_update(s1)
print("s1 : ", s1)
print("s2 : ", s2)


#output

s1 : {40, 10, 50, 20, 30}
s2 : {
10, 30}
s1 : {
40, 10, 50, 20, 30}
s2 : {
40, 50, 20}














Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India