×

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 abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Write Dictionary to CSV 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...

4 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 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 program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Import py file in Python

In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module...

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

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Creating Tables using Python MySQL

In this article, we are going to learn how to create tables in databases using Python MySQL. Introduction to Tables: Generally, databases are used in order to store the information in the...

9 minutes read.

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image. Earlier detection...

4 minutes read.

Python Unit Test Cheat String

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.