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 discard() method with examples

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

The discard() method in Python removes a specified element from the Set. It works same as remove() method, however there is a difference between remove() and discard() method. The remove() method raises an error when the specified element doesn’t exist in the given set, however the discard() method doesn’t raise any error if the specified element is not present in the set and the set remains unchanged.

Python Set discard() method syntax

set.discard(item)

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

Python Set discard() method example

In the following example we have a set of numbers and we are removing few elements from the given set using discard() method.

# A Set of numbers
numbers = {1, 2, 3, 4, 5}

# displaying the set before removing anything
print("Original Set is:", numbers)

# Element 3 & 4 are removed from the Set
numbers.discard(3)
numbers.discard(4)

# displaying the set after discard() method
print("Updated Set is:", numbers)

Output:
Python discard() method example

If element doesn’t exist in the Set

If the specified element does not exist in the given Set then this method doesn’t raise any error and the set remains unchanged. Lets take an example to see this in action.

# A Set of numbers
numbers = {1, 2, 3, 4, 5}

# displaying the set before removing anything
print("Original Set is:", numbers)

# trying to remove the element 99 from the set
numbers.discard(99)

# displaying the set after discard() method
print("Updated Set is:", numbers)

Output:
Python discard() method, specified element is not present

As you can see in the output that no errors were raised and the set remained unchanged when we tried to remove an element from the set which was not present. If you want the error to be raised in such cases then use the remove() 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 add() 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