Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Python set discard method

Python set discard function is useful to discard or remove an item from the given set. The syntax this set discard function is:

set_Name.discard(element)

Python set discard Example 1

The set discard function is the same as remove function. This set function helps you to discard or remove an item from a given set. It is a very useful set function if you know the item you want to discard. The below code discards or remove 30 from a given set called discardSet

TIP: Please refer to the Python set article to understand everything about Sets in Python.

# Python set discard method

discardSet = {10, 25, 30, 45, 50}
print("\nOld Set Items = ", discardSet)

# Python Discard Set Item
discardSet.discard(30)
print("After Discard Method - Set Items = ", discardSet)
Python set discard method 1

How to discard set item Example 2

In this example, first, we declared a string set. Next, we used this set discard method on this string set. Here, discard function discard or remove the orange from the discard fruits set.

# Python set discard method

discardFruitSet = {'apple', 'mango', 'cherry', 'kiwi', 'orange', 'banana'}
print("\nOld Set Items = ", discardFruitSet)

# Python Discard Set Item
discardFruitSet.discard('orange')
print("After Discard Method = ", discardFruitSet)
Python set discard method 2

How to discard set item Example 3

As we said earlier, this Python set discard function is the same as the remove method. However, if we are discarding non-existing items, this function does not throw an error.

In this set discard example, we tried to remove Berry from the fruits set.

# Python set discard method

discardFruitSet = {'apple', 'mango', 'cherry', 'kiwi', 'orange', 'banana'}
print("\nOld Set Items = ", discardFruitSet)

# Python Discard Set Item
discardFruitSet.discard('orange')
print("After Discard Method = ", discardFruitSet)

# Python DIscard Non existing set item
discardFruitSet.discard('Berry')
print("After Discard Method = ", discardFruitSet)
Python set discard method 3

Filed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy