DEV Community

Main
Main

Posted on • Updated on • Originally published at pynerds.com

Set methods in Python

 

set is an abstract data type that represents an unordered collection of unique elements.

Sets just like lists are mutable meaning that operations such as insertion and deletion can be performed on them in-place and without  creating a new set. 

In Python, sets are represented using the  set type/class. This class defines a number of methods to support various operations on sets. The operations can be sub-divided into: 

  1. Elementary operations such as insertion, deletion, etc.
  2. Set operations such as union, intersection, difference, etc .

Methods for Elementary operations

Set, like most data structures, supports basic operations such as Insertion and deletion. This is achieved through several methods defined in the set class.

add()

The add() method is used to add a single element to the set. It takes one parameter, which is the element to be added

myset.add(x)
x The element to be added to the set.

Sets can only contain unique elements thus the operation will have no effect if the element already exists.

myset = {1, 2, 3}

#add some elements
myset.add(4)
myset.add(5)

#this has no effect because the elements exists.
myset.add(2)
myset.add(1)

print(myset)

clear() 

The clear() method takes no argument, it removes all elements present in a set effectively turning it to an empty set.

myset.clear()
#a non-empty set
myset = {1, 2, 3, 4}

#remove all elements
myset.clear()
print(myset)

copy()

The copy() method is used to create a shallow copy of the set.This means that  the method returns a new set object that contains all the elements of the original set but will not be affected if any changes are made to the original set.

The method takes does not take any arguments.

myset.copy()

The method returns a new set which is a shallow copy of the original set.

myset = {3, 5, 7}

copied = myset.copy()
print(copied)

discard()

The discard() method removes an element from the set if  it is present. It does not raise an exception if  the element is not present.

myset.discard(x)
x The element to be removed.
myset = {1, 3, 5, 6, 7, 8, 9}

#remove an element
myset.discard(6)
myset.discard(8)

print(myset)

#no exception is raised if the element does not exist
myset.discard(10)

pop() 

The pop() method removes and returns an arbitrary/random element from the set. It raises a KeyError if the set is empty.

myset.pop()

The method takes no arguments, it returns the removed arbitrary element.

myset = {0, 2, 4, 6, 8, 10}

#remove and return an arbitrary element
print(myset.pop())
print(myset.pop())
print(myset.pop())
print(myset.pop())

print(myset)

remove() 

The remove() method just like discard() , removes an element from a set if it is a member. However, if the element is not present, remove() will raise a KeyError whereas discard() will not.

myset.remove(x)
x The element to be removed.

The method removes element x from the set , it raises a KeyError exception if the element is not present.

myset = {1, 3, 5, 6, 7, 8, 9}

myset.remove(6)
myset.remove(8)
print(myset)

#raises an exception
myset.remove(10)

update() 

The update()  method  is used to extend a set  with elements from an iterable object. This means elements from the iterable that  are not already present in the set will be added to the set.

myset.extend(iterable)
iterable An iterable object containing the elements to be added to the set.
myset = {1, 3, 5}

#The iterable with the elements
elements = [5, 7, 9, 11]

#add elements to the set
myset.update(elements)
print(myset)

Methods for set operations 

Apart from the usual operations, sets also supports operations that are only unique to them. Such operations includes Union, intersection,  Difference, etc.

In this part we will explore the various methods that are meant for performing set operations.

difference()

The difference() method returns the difference of two or more sets as a new set.  The returned set contains only those items from the first set that are not present in the other sets.

set1.difference(set2 [,set3] [, set4], ...)

The method takes in arbitrary number of sets as arguments and returns a set with the elements of the original set(set1) which are not present in the input sets. Note that if multiple sets are given as inputs,  they will be combined into one set before the operation.

set1 = {1, 3, 5, 7, 9, 11}
set2 = {3, 9, 13}
set3 = {7, 9, 11}

diff = set1.difference(set2, set3)
print(diff)

difference_update()

The difference_update() method is similar to the difference() method in that  both returns those elements from one set that are not present in the other(s). However, the  difference_update() method updates the original set by removing elements found in the second set rather than creating a new set.

set1.difference_update(set2 [,set3][, set4]...)

The method removes elements from set1 that are present in the input sets(set2, set3, etc).

set1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set2 = {0, 2, 4}
set3 = {6, 8}

set1.difference_update(set2, set3)
#elements are removed from the original set
print(set1)

intersection()

The intersection() method returns the intersection of two sets as a new set. This is a set that contains all the elements that are common between  two given sets.

set1.intersection(set2 [,set3] [,set4]...)

The method returns elements that are common in both the original set and the input set.

set1 = {1, 2, 3, 4, 5}
set2 = {1, 3, 5, 7, 9}

result = set1.intersection(set2)
print(result)

intersection_update()

This method is just like the intersection() method except that it  does not create a new set, instead, it updates the original set.  

s1 = {1,2,3,4,5} 
s2 = {3,4,5,6,7} 
s1.intersection_update(s2)

print(s1)

symmetric_difference() 

The symmetric_difference() method returns a new set with elements that are in either of the sets, but not in both.

set1.symmetric_difference(set2) 
s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

result = s1.symmetric_difference(s2)
print(result)

symmetric_difference_update()

Just like symmetric_difference() except that it updates the original list rather than creating a new one.

set1.symmetric_difference_update(set2) 
s1 = {'a', 'b', 'c', 5}
s2 = {3, 5, 7, 'c'}

s1.symmetric_difference_update(s2)
print(s1)

isdisjoint()

The isdisjoint() method in checks whether two sets have a common item or not. If there are common elements in sets, it returns False otherwise it will return True.

set1.isdisjoint(set2)

It returns True if there is no any common element between set1 and set2, otherwise False

s1 = {'a', 'b', 'c'}
s2 = {1, 2, 3, 4}
s3 = {0, 2, 4}

print(s1.isdisjoint(s2))
print(s1.isdisjoint(s3))
print(s2.isdisjoint(s3))

issubset() 

The issubset() method checks if all elements of a set are present in another set (passed as an argument). 

set1.issubset(set2)

It returns True if all elements in set1 are present in set2, otherwise False.

evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(evens.issubset(digits))
print(odds.issubset(digits))
print(evens.issubset(odds))

issuperset()

The issuperset() method checks whether the current set is a superset of the input set. That is if all the elements in the input set are contained within the current set. 

set1.issuperset(set2)

The method returns True if all elements of set2 are in set1, otherwise False.

evens = {0, 2, 4, 6, 8}
odds = {1, 3, 5, 7, 9}
digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

print(digits.issuperset(evens))
print(digits.issuperset(odds))
print(evens.issuperset(odds))

Related articles


Top comments (0)