×

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

Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Check if a String is Empty in Python

The string is one of the data types of Python. This data type stores all kinds of data but all the characters must be enclosed using double quotes (""). In...

5 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

Python String endswith() method

Python String endswith() method The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False. Syntax endswith(suffix[, start[, end]]) Parameter suffix – This parameter represents a string or tuple...

2 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.

Python Set clear() Method

Python Set clear() Method The set.clear() method removes all the elements from the set. Syntax set.clear() Parameter NA Return None Example 1 # Python program explaining # the set.clear() method # initializing the set fruits = {"watermelon", "banana", "apple"} # printing the set...

2 minutes read.

How to Concatenate Two Strings in Python

How to Concatenate Two Strings in Python Like the other data types, operations on strings are quite useful when we deal with some real-life applications. Here we will talk about a simple...

4 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

How to convert integer to float in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

5 minutes read.