×

Python Set remove() method

Python Set remove() method

The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does not exist.

Syntax

set.remove(element)

Parameter

element- This parameter signifies the item to search for and later to  remove it.  

Return

This method returns the specified element from the set.

Example 1

# Python program explaining
# the set.remove() method
#initializing set1
set1 = {"mango", "banana", "apple", "tomato"}
print("Actual Set 1:",set1) 
# removing "banana" item from the set 
value= set1.remove("banana") 
# printing the removed value  
print("Element removed from set:",value) 
# printing the set after pop method
print("After the removing the value...\nNew Set: ", set1) 

Output

Actual Set 1: {'mango', 'tomato', 'banana', 'apple'}
Element removed from set: None
After the removing the value
New Set:  {'mango', 'tomato', 'apple'} 

Example 2

# Python program explaining
# the set.remove() method
#initializing set1
set1 = {1, 2,3,4,5,6,7} 
print("Actual Set 1:",set1)
# removing "banana" item from the set 
value= set1.remove() # passing no element
# will pass an TypeError as remove() takes one argument 
print("Element removed from set:",value) 
# printing the set after pop method
print("After the removing the value...\nNew Set: ", set1) 

Output

Actual Set 1: {1, 2, 3, 4, 5, 6, 7}
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    value= set1.remove() # passing no element
TypeError: remove() takes exactly one argument (0 given) 

Example 3

# Python program explaining
# the set.remove() method
# initializing vowel set
vowel = {'a', 'e', 'i', 'o', 'u'}
# Deleting 'z' element
vowel.remove('z') # the element 'z' is not present in the sent 
# Updated vowel set
# will return a KeyError as the element 'z' is not present
print('Updated vowel set: ', vowel) 

Output

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    vowel.remove('z') # the element 'z' is not present in the sent
KeyError: 'z' 

Related Topics

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Static Variables in Python

What is a Static Variable? The variable that remains with a constant value throughout the program or throughout the class is known as a " Static Variable ". Static variables are...

3 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.