Set Methods

add(): Add an element to a set.

SYNTAX:

set.add(element)

Example:

setA = {1,2,3,4}
print(setA)
setA.add(5)
print(setA)

Output:

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

clear(): Remove all the elements from the set.

SYNTAX:

set.clear()

Example:

setA = {1,2,3,4}
print(setA)
setA.clear()
print(setA)

Output:

{1, 2, 3, 4}
set()

copy(): Returns a shallow copy of set.

SYNTAX:

set.copy()

Example:

setA = {1,2,3,4}
setB = setA.copy()
print(setB)

Output:

{1,2,3,4}

difference(): Returns the difference of two or more sets as a new set.

SYNTAX:

set1.difference(set2)

Example:

setA = {1,2,3,4}
setB = {4,5,6}
setC = {3,7,8}
print(setA.difference(setB,setC))

Output:

{1, 2}

difference_update(): Removes all the elements of another set from this set.

SYNTAX:

set1.difference_update(set2)

Example:

setA = {1,2,3,4}
setB = {4,5,6}
print("setA :",setA)
print("setB :",setB)
setA.difference_update(setB)
print("After difference, setA :",setA)

Output:

setA : {1, 2, 3, 4}
setB : {4, 5, 6}
After difference, setA : {1, 2, 3}

discard(): Removes an element from the set if it is a member. Do nothing if element is not in set.

SYNTAX:

set.discard(element)

Example:

setA = {1,2,3,4}
setA.discard(3) #Deletes element 3 from setA
setA.discard(5) #Discard the operation because element 5 is not a member of setA
print(setA)

Output:

{1, 2, 4}

intersection(): Returns intersection of two or more sets as new set.

SYNTAX:

set1.intersection(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6,7}
setC = {3,4,5,6}
print(setA.intersection(setB,setC))

Output:

{3, 4}

intersection_update(): Update the set with the intersection of itself and another.

SYNTAX:

set1.intersection_update(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
setA.intersection_update(setB)
print("After intersection setA :",setA)

Output:

{3, 4}

isdisjoint(): Return True if two sets have null intersection. False otherwise.

SYNTAX:

set1.isdisjoint(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
setC = {5,6,7,8}
print("setA :",setA)
print("setB :",setB)
print("setC :",setC)
print("setA is disjoint of setB",setA.isdisjoint(setB))
print("setA is disjoint of setC",setA.isdisjoint(setC))

Output:

setA : {1, 2, 3, 4}
setB : {3, 4, 5, 6}
setC : {8, 5, 6, 7}
setA is disjoint of setB False
setA is disjoint of setC True

issubset(): Returns True if another set contains this set. False otherwise.

SYNTAX:

set1.issubset(set2)

Example:

setA = {1,2,3,4}
setB = {1,2,3,4}
setC = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
print("setC :",setC)
print("setA is subset of setB",setA.issubset(setB))
print("setA is subset of setC",setA.issubset(setC))

Output:

setA : {1, 2, 3, 4}
setB : {1, 2, 3, 4}
setC : {3, 4, 5, 6}
setA is subset of setB True
setA is subset of setC False

issuperset(): Returns True if this set contains another set. False otherwise.

SYNTAX:

set1.issuperset(set2)

Example:

setA = {1,2,3,4}
setB = {3,4}
setC = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
print("setC :",setC)
print("setA is superset of setB",setA.issuperset(setB))
print("setA is superset of setC",setA.issuperset(setC))

Output:

setA : {1, 2, 3, 4}
setB : {3, 4}
setC : {3, 4, 5, 6}
setA is superset of setB True
setA is superset of setC False

pop(): Remove and return an arbitary set element. Raises TypeError if the set empty.

SYNTAX:

set.pop()

Example:

setA = {1,2,3,4}
setB = {}
print("Popped element from setA :",setA.pop())
print("Popped element from setB :",setB.pop())

Output:

Popped element from setA : 1

Traceback (most recent call last):
  File "jdoodle.py", line 4, in <module>
    print("Popped element from setB :",setB.pop())
TypeError: pop expected at least 1 arguments, got 0

remove(): Remove an element from set. If the element is not a member, raises a KeyError.

SYNTAX:

set.remove(element)

Example:

setA = {1,2,3,4}
setA.remove(3)
print("setA :",setA)
setA.remove(5)

Output:

setA : {1, 2, 4}

Traceback (most recent call last):
  File "jdoodle.py", line 4, in <module>
    setA.remove(5)
KeyError: 5

symmetric_difference(): Returns symmetric difference of two sets as a new set.

SYNTAX:

set1.symmetric_difference(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
print("Symmetric difference between setA and setB :", setA.symmetric_difference(setB))

Output:

setA : {1, 2, 3, 4}
setB : {3, 4, 5, 6}
Symmetric difference between setA and setB : {1, 2, 5, 6}

symmetric_difference_update(): Update a set with the symmetric difference of itself and another.

SYNTAX:

set1.symmetric_difference_update(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
setA.symmetric_difference_update(setB)
print("After Symmetric difference setA :",setA)

Output:

setA : {1, 2, 3, 4}
setB : {3, 4, 5, 6}
After Symmetric difference setA : {1, 2, 5, 6}

union(): Returns the union of sets as a new set.

SYNTAX:

set1.union(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
print("setA.union(setB) :",setA.union(setB))

Output:

setA : {1, 2, 3, 4}
setB : {3, 4, 5, 6}
setA.union(setB) : {1, 2, 3, 4, 5, 6}

update(): Update a set with the union of itself and other.

SYNTAX:

set1.update(set2)

Example:

setA = {1,2,3,4}
setB = {3,4,5,6}
print("setA :",setA)
print("setB :",setB)
setA.update(setB)
print("After update setA :",setA)

Output:

setA : {1, 2, 3, 4}
setB : {3, 4, 5, 6}
After update setA : {1, 2, 3, 4, 5, 6}