Set Methods python

Set Methods python

In the previous tutorial, you learned how to use in-built dictionary methods in your Python program. But, In this post, you will learn python built-in set methods/functions.

If you want to know more about python sets click here.

Python Sets built-in Functions/Methods

Python provides many in-built methods/functions for the sets, which is works with python set datatype. And you can modify and manipulate the python sets by using built-in sets methods/functions of python.

We have provided a list below. Almost all built-in sets methods/functions python are there:

MethodDescription
add()Adds an element to the set
clear()Removes all the elements from the set
copy()Returns a copy of the set
difference()Returns a set containing the difference between two or more sets
difference_update()Removes the items in this set that are also included in another, specified set
discard()Remove the specified item
intersection()Returns a set, that is the intersection of two other sets
intersection_update()Removes the items in this set that are not present in other, specified set(s)
isdisjoint()Returns whether two sets have a intersection or not
issubset()Returns whether another set contains this set or not
issuperset()Returns whether this set contains another set or not
pop()Removes an element from the set
remove()Removes the specified element
symmetric_difference()Returns a set with the symmetric differences of two sets
symmetric_difference_update()inserts the symmetric differences from this set and another
union()Return a set containing the union of sets
update()Update the set with the union of this set and others

Python Set add()

The set add() method adds a given element to a set. If the element is already present, it doesn’t add any element.

# set of numbers
num = {1, 2, 3, 4}

# adding '5'
num.add(5)
print('numbers are:', num)

# adding '5' again
num.add(5)
print('numbers are:', num)

#output
#numbers are: {1, 2, 3, 4, 5}
#numbers are: {1, 2, 3, 4, 5}

Python Set clear()

The clear() method is used to remove all elements from the set.

# set of numbers
num = {1, 2, 3, 4}

# clear list
num.clear()

print('numbers are:', num)

#Output
#numbers are: set()

Python Set copy()

The copy() method is used to copy the set.

# set of numbers
num = {1, 2, 3, 4}

# copy list
x = num.copy()
print('numbers are:', x)

#output
#numbers are: {1, 2, 3, 4}

Python Set remove()

The remove() method is used to search for the given element in set list and remove it.

# set of numbers
num = {1, 2, 3, 4}

# remove element from list
num.remove(2)

print('numbers are:', num)

#Output
#numbers are: {1, 3, 4}

Python Set discard()

The discard() method is used to remove a specific element from the set. If the element present in the python set list.

numbers = {2, 3, 4, 5}

numbers.discard(3)
print('numbers = ', numbers)

numbers.discard(10)
print('numbers = ', numbers)

#outout
#numbers =  {2, 4, 5}
#numbers =  {2, 4, 5}

Python Set pop()

The pop method is used to remove a random element from the set.

# set of numbers
st = {'a', 'v', 'c', 'g', 'j'}

# remove element from list
st.pop()

print('elements are:', st)

#output
#numbers are: elements are: {'c', 'g', 'a', 'j'}

Python Set difference()

The difference() method is used to get the difference between two python sets.


Python Set update()

The python set update() method is used to add elements/items from another set.

x = {"1", "2", "3"}
y = {"4", "5", "6"}

x.update(y) 

print(x)

#Output
#{'2', '4', '6', '3', '1', '5'}

Python Set isdisjoint()

The isdisjoint() method returns True if two sets are disjoint sets. If not, it returns False.

A = {1, 2, 3, 4}
B = {5, 6, 7}
C = {4, 5, 6}

print('Are A and B disjoint?', A.isdisjoint(B))
print('Are A and C disjoint?', A.isdisjoint(C))

#Output
#Are A and B disjoint? True
#Are A and C disjoint? False

Python Set intersection()

The intersection () method uses two or more sets to extract a similar element and create a new set.

x = {1, 2, 3}
y = {3, 4, 5}

z = x.intersection(y) 

print(z)

#output
#3

Python Set issubset()

The issubset() method returns results True. If all elements of a set are present in another set. If is not present in another set, it returns False.

A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}

# Returns True
print(A.issubset(B))

# Returns False
print(B.issubset(A))

Python Set symmetric_difference()

The Python symmetric_difference() method is used to get the symmetric difference of two sets.

x = {'x', 'y', 'c', 'd'}
y = {'c', 'd', 'e' }
z = {}

print(x.symmetric_difference(y))
print(y.symmetric_difference(x))

print(x.symmetric_difference(z))
print(y.symmetric_difference(z))

#output
#{'x', 'e', 'y'}
#{'x', 'e', 'y'}
#{'x', 'c', 'y', 'd'}
#{'d', 'e', 'c'}

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *