×

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

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

apply() function in python

Pandas: Pandas is a library in python. It is an open source package in python. Pandas in python are used for data cleaning and data analysis. Data frame mainly consists of...

3 minutes read.

Python open() function

Python open() function The open() function in Python opens a file and returns a corresponding file object. If the file cannot be opened, an OSError is raised. Syntax open(file, mode='r', buffering=1, encoding=None, errors=None, newline=None, closefd=True, opener=None) Parameter file It represents the path and name of the file mode...

2 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 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.

What does base case mean in recursion

Base cases and recursive stages are used to define recursive functions. In a primary subject, we compute the outcome right away, given the function call's arguments. In a recursive step, we...

4 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Python Variables, Constants and Literals

Python is today's most valuable, easy-to-implement and sought-out programming language. Nowadays, developers want to focus on implementing the program rather than spending time with complex programs. So, for this reason,...

17 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

Google Python Class

Python: 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 faster way....

3 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.