BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set remove() Method with examples

Last Updated: March 30, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The remove() method in Python searches for the specified element in the given Set and removes it. If the specified element does not exist in the Set then an error is raised.

Python Set remove() method syntax

set.remove(item)

Parameter: Here item is the element which we want to remove the set.
Return value: This method doesn’t return anything.

Python Set remove() method example

In the following example we have a Set of names and we are removing an element from this given set using the remove() method.

# A Set of names
names = {"Rick", "Negan", "Glenn"}

# Original Set
print("Original Set is:", names)

# Element "Negan" is removed from the Set
names.remove("Negan")

# Set after removing an element from it
print("Updated Set is:", names)

Output:
Python remove() method example

When element doesn’t exist in the Set

If you try to remove an element from the Set which doesn’t exist then an error is raised. Lets take an example to see this behaviour of remove() method.

Here we are trying to remove a name “Chaitanya” from the given set of names but the element does not exist in the set names, thus an error is raised as shown in the output below.

# A Set of names
names = {"Rick", "Negan", "Glenn"}

# Original Set
print("Original Set is:", names)

# Trying to remove element "Chaitanya"
names.remove("Chaitanya")

# Set after removing an element from it
print("Updated Set is:", names)

Output:

Original Set is: {'Glenn', 'Negan', 'Rick'}
Traceback (most recent call last):
  File "/Users/chaitanyasingh/PycharmProjects/BeginnersBookProJ/venv1/BeginnersBookDemo.py", 
line 8, in 
    names.remove("Chaitanya")
KeyError: 'Chaitanya'

Process finished with exit code 1

As you can see a KeyError is raised when we tried to remove an element from the set which was not present. If you do not want any error to be raised in such cases then use discard() method instead.

Top Related Articles:

  1. Python Recursion
  2. Python Data Types
  3. Python Keywords and Identifiers with examples
  4. Python Constructors – default and parameterized
  5. Python Set clear() Method with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap