Python - Remove Set Items



Python's set class provides different methods to remove one or more items from a set object.

Remove Set Items

The remove() method removes the given item from the set collection, if it is present in it. However, if it is not present, it raises KeyError.

Syntax

set.remove(obj)

Parameters

  • obj − an immutable object

Example

lang1 = {"C", "C++", "Java", "Python"}
print ("Set before removing: ", lang1)
lang1.remove("Java")
print ("Set after removing: ", lang1)
lang1.remove("PHP")

It will produce the following output

Set before removing: {'C', 'C++', 'Python', 'Java'}
Set after removing: {'C', 'C++', 'Python'}
   lang1.remove("PHP")
KeyError: 'PHP'

Remove Set Items Ignoring Error

The discard() method in set class is similar to remove() method. The only difference is, it doesn't raise error even if the object to be removed is not already present in the set collection.

Syntax

set.discard(obj)

Parameters

  • obj − An immutable object

Example

lang1 = {"C", "C++", "Java", "Python"}
print ("Set before discarding C++: ", lang1)
lang1.discard("C++")
print ("Set after discarding C++: ", lang1)
print ("Set before discarding PHP: ", lang1)
lang1.discard("PHP")
print ("Set after discarding PHP: ", lang1)

It will produce the following output

Set before discarding C++: {'Java', 'C++', 'Python', 'C'}
Set after discarding C++: {'Java', 'Python', 'C'}
Set before discarding PHP: {'Java', 'Python', 'C'}
Set after discarding PHP: {'Java', 'Python', 'C'}

Remove Random Item from Set

The pop() method in set class removes an arbitrary item from the set collection. The removed item is returned by the method. Popping from an empty set results in KeyError.

Syntax

obj = set.pop()

Return value

The pop() method returns the object removed from set.

Example

lang1 = {"C", "C++"}
print ("Set before popping: ", lang1)
obj = lang1.pop()
print ("object popped: ", obj)
print ("Set after popping: ", lang1)
obj = lang1.pop()
obj = lang1.pop()

It will produce the following output

Set before popping: {'C++', 'C'}
object popped: C++
Set after popping: {'C'}
Traceback (most recent call last):
   obj = lang1.pop()
         ^^^^^^^^^^^
KeyError: 'pop from an empty set'

At the time of call to pop() for third time, the set is empty, hence KeyError is raised.

Remove All Set Items

The clear() method in set class removes all the items in a set object, leaving an empty set.

Syntax

set.clear()

Example

lang1 = {"C", "C++", "Java", "Python"}
print (lang1)
print ("After clear() method")
lang1.clear()
print (lang1)

It will produce the following output

{'Java', 'C++', 'Python', 'C'}
After clear() method
set()

Remove Items that Exist in Both Sets

The difference_update() method in set class updates the set by removing items that are common between itself and another set given as argument.

Syntax

set.difference_update(obj)

Parameters

  • obj − a set object

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1 before running difference_update: ", s1)
s1.difference_update(s2)
print ("s1 after running difference_update: ", s1)

It will produce the following output

s1 before running difference_update: {1, 2, 3, 4, 5}
s1 after running difference_update: {1, 2, 3}
set()

Remove Items that Exist in Either Set

The difference() method is similar to difference_update() method, except that it returns a new set object that contains the difference of the two existing sets.

Syntax

set.difference(obj)

Parameters

  • obj − a set object

Return value

The difference() method returns a new set with items remaining after removing those in obj.

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.difference(s2)
print ("s3 = s1-s2: ", s3)

It will produce the following output

s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s3 = s1-s2: {1, 2, 3}

Remove Uncommon Set Items

As a result of intersection_update() method, the set object retains only those items which are common in itself and other set object given as argument.

Syntax

set.intersection_update(obj)

Parameters

  • obj − a set object

Return value

The intersection_update() method removes uncommon items and keeps only those items which are common to itself and obj.

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s1.intersection_update(s2)
print ("a1 after intersection: ", s1)

It will produce the following output

s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 after intersection: {4, 5}

The intersection() method in set class is similar to its intersection_update() method, except that it returns a new set object that consists of items common to existing sets.

Syntax

set.intersection(obj)

Parameters

  • obj − a set object

Return value

The intersection() method returns a set object, retaining only those items common in itself and obj.

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.intersection(s2)
print ("s3 = s1 & s2: ", s3)

It will produce the following output

s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s3 = s1 & s2: {4, 5}

Symmetric Difference Update of Set Items

The symmetric difference between two sets is the collection of all the uncommon items, rejecting the common elements. The symmetric_difference_update() method updates a set with symmetric difference between itself and the set given as argument.

Syntax

set.symmetric_difference_update(obj)

Parameters

  • obj − a set object

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s1.symmetric_difference_update(s2)
print ("s1 after running symmetric difference ", s1)

It will produce the following output

s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 after running symmetric difference {1, 2, 3, 6, 7, 8}

Symmetric Difference of Set Items

The symmetric_difference() method in set class is similar to symmetric_difference_update() method, except that it returns a new set object that holds all the items from two sets minus the common items.

Syntax

set.symmetric_difference(obj)

Parameters

  • obj − a set object

Return value

The symmetric_difference() method returns a new set that contains only those items not common between the two set objects.

Example

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print ("s1: ", s1, "s2: ", s2)
s3 = s1.symmetric_difference(s2)
print ("s1 = s1^s2 ", s3)

It will produce the following output

s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8}
s1 = s1^s2 {1, 2, 3, 6, 7, 8}
Advertisements