4 methods to Remove element from Python Set

Set

In this article, we are going to learn about 4 methods to Remove elements from Python Set, all are in-built methods of Python Set to Remove elements (remove, discard, pop, clear). If you are wondering, how to delete elements from the python Set here is the answer. You can use the Built-in functions to do this job. We will cover these functions in this article, so you know, what to do to delete elements from a set.

These are the Set inbuilt function of the set to remove elements from Set.

  • Remove(): Remove an element from Set.
  • Discard(): Remove an element from Set
  • POP(): Remove an arbitrary(random) element
  • Clear() :Remove all elemnts

Let us understand each of them with a code examples

1. Python Set Remove()

It removes the given element from the Set. If a passed element does not exist in Set, It raises a KeyError.

Syntax

Set.remove(element)

Parameter

Element: It takes a single required parameter,indicate the element we want to delete.

Return Value

It removes the given element from the Set, and update the set. It returns None (nothing).

How to Remove an element From Python Set using Remove()

first_set = {'evening','Hi',5,6,7,'Good'}

#removing element using remove() method

first_set.remove(5)


print('set after removing element :',first_set)

Output

set after removing element : {6, 7, 'evening', 'Good', 'Hi'}

Example:When delete not exist Element using Remove()

In this code example, we are removing an element that does not exist in Set.As we can see it throwing keyError.

first_set = {'evening','Hi',5,6,7,'Good'}

#removing not exist element using remove() method

first_set.remove(10)


print('set after removing element :',first_set)

Output

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    first_set.remove(10)
KeyError: 10

2. Set Discard()


It removes the given element from the set. If an element does not exist it will not raise an error. Instead of remove(), we can use this to avoid an error.

Syntax

set.remove(ele)

Parameters

Element : It takes a single element as argument that we want to delete.

Return Values

It return None and remove the given element from the SET and update Set.

Example: Remove an element From Set using Discard()

fruit_Set = {'apple','kiwi',5,6,7,'Fig'}

#removing  element using discard() method

fruit_Set.discard(6)


print('set after removing element :',fruit_Set)

Output

set after removing element : {5, 'kiwi', 7, 'apple', 'Fig'}

Code Example :Remove not exist Element

In this example we are removing the element that does not exist in Set. Discard does not throw any exception.

fruit_Set = {'apple','kiwi',5,6,7,'Fig'}

#removing not exist element using discard() method

print(fruit_Set.discard(8))


print('set after removing element :',fruit_Set)

Output

None
set after removing element : {'kiwi', 5, 'apple', 6, 7, 'Fig'}

3. Python Set Pop()


It removes an arbitrary(random) element from the Set and returns deleted element. It does not take any argument.

Syntax

set.pop()

Parameter

Pop method does not take any argument.

Return Value

It returned an arbitrary deleted element from the set and update the set. The element no longer exists in SET.

Note : If the set is empty, it raises TypeError Exception.

Example : How to Remove an element from Python with Pop()

The Pop() remove element 5,because it choose randomly.

fruit_Set = {'apple','kiwi',5,6,7,'Fig'}

#removing  element using pop() method

print('deleted element : ',fruit_Set.pop())


print('set after removing element :',fruit_Set)

Output

Note:The output might be different because Pop() delete arbitrary ( random) element.

deleted element :  5
set after removing element : {6, 7, 'kiwi', 'apple', 'Fig'}

4. Set Clear()


The clear() in-built method of set removes all elements of the Set. It does not take any argument.

Syntax

set.clear()

Parameters

The Python set clear method does not take any argument.

Return value

It returns none(nothing)

Example: Remove all elements of Set using Clear() method

We are checking the value return by clear() method, as we can see it is returning None. The Set is empty after using the clear() method.

fruit_Set = {'apple','kiwi',5,6,7,'Fig'}

#removing all elements of the set using the clear() set method

print(' value return by clear method : ',fruit_Set.clear())


print('set after clear elements :',fruit_Set)

Output

 value return by clear method :  None
set after clear elements : set()