How to Use Set discard() Method in PythonPython Basics


The discard() method finds the given element in the set and removes it.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

Car1 = {"Mercedes-Benz", "Rolls-royce", "CADILLAC","Honda","Hyundai","Land Rover"}
print(Car1)
Car1.discard("Rolls-royce")
print(Car1)

Output:

{'Rolls-royce', 'Mercedes-Benz', 'Land Rover', 'CADILLAC', 'Honda', 'Hyundai'}
{'Mercedes-Benz', 'Land Rover', 'CADILLAC', 'Honda', 'Hyundai'}

Syntax

set.discard(x)

Parameters

Name Description
x The given element to be removed.

Return Value

The discard(x) removes element x from the set if the element is present.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us