Set Methods in Python

 Set Methods In Python

Python Set Methods


http://www.entertainmentearth.com/cjdoorway.asp?url=hitlist.asp?theme=Black+Panther

Sets similarly to list have also built in methods to use on sets however these methods  are different and I'll explain them to you with examples.

 

Built in Set Methods in Python:

add() set.add(element)
Adds an element to the set.
animals  = {"goat", "monkey", "chicken"}

animals.add("sheep")

print(animals)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'chicken', 'sheep', 'monkey', 'goat'}
 

clear() set.clear()
Removes all the elements from the set.
animals  = {"goat", "monkey", "chicken", 'sheep'}

animals.clear()

print(animals)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

set()
 

copy() set.copy()
Returns a copy of the set. 
animals  = {"goat", "monkey", "chicken", 'sheep'}

animals2= animals.copy()

print(animals)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'goat', 'monkey', 'sheep', 'chicken'}
 

difference() set.difference(set)
Returns a set containing the difference between two or more sets. The returned difference is sets with elements that exist only in first set.
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}

diff = set1.difference(set2)

print(diff)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

difference_update() set.difference_update(set)
Removes the items in this set that are also included in another, specified set. 
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}

set1.difference_update(set2)

print(set1)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'python', 'monkey'}
 

discard() set.discard(value)
Remove the specified item.

animals  = {"python", "monkey", "goat", "horse"}

animals.discard('horse')

print(animals)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'goat', 'python', 'monkey'}
 

intersection() set.intersection(set1....)
Returns a set, that is the intersection of two other sets.
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}

inter = set1.intersection(set2)

print(inter)
__________________________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'goat'}
 

intersection_update() set.intersection_update(set1....)
Removes the items in this set that are not present in other, specified set(s).
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}

set1.intersection_update(set2)

print(set1)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'goat'}

isdisjoint() set.isdisjoint(set)
Returns whether two sets have a intersection or not. 
set1 = {"python", "monkey", "goat"}
set2 = {"ford", "bmw", "mercedes"}

c = set1.isdisjoint(set2)

print(c)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

True

issubset() set.issubset(set)

Returns whether another set contains this set or not
 
animals = {"python", "monkey", "goat"}
set2 = {"ford", "python", "bmw", "mercedes","monkey", "goat"}

c = animals.issubset(set2)

print(c)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

True
issuperset() set.issuperset(set)
Returns whether this set contains another set or not. 
set1 = {"ford", "python", "bmw", "mercedes","monkey", "goat"}

animals = {"python", "monkey", "goat"}

c = set1.issuperset(animals)

print(c)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

True
 

pop() set.pop()
Removes first element from the set
animals = {"python", "monkey", "goat"}

animals.pop()

print(animals)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'monkey', 'goat'}
 

remove() set.remove(item)
Removes the specified element. 
animals = {"python", "monkey", "goat"}

animals.remove("goat")

print(animals)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'python', 'monkey'}
 
symmetric_difference() set.symmetric_difference(set)
Returns a set with the symmetric differences of two setst. 
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}


a = set1.symmetric_difference(set2)

print(a)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'monkey', 'cow', 'chicken', 'python'}
 

symmetric_difference_update() set.symmetric_difference_update(set)
inserts the symmetric differences from this set and another. 

set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}


set1.symmetric_difference_update(set2)

print(set1)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'cow', 'python', 'monkey', 'chicken'}
 

union() set.union(set1, set2)
Return a set containing the union of sets. 
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}


a = set1.union(set2)

print(a)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'chicken', 'python', 'goat', 'monkey', 'cow'}
 

update() set.update(set)
Update the set with the union of this set and others. 
set1 = {"python", "monkey", "goat"}
set2 = {"cow", "chicken", "goat"}


set1.update(set2)

print(set1)
_____________________________________________________
/usr/bin/python3.8 https://viteac.blogspot.com

{'python', 'cow', 'monkey', 'chicken', 'goat'}
 

Comments