discard() method of set

All set methods

discard(element) takes one argument .

element : matching element to be discardd from set
Returns : No return value.
my_set={'a','b','c','x','k'}
my_set.discard('x')
print(my_set)
Output is here
{'c', 'a', 'k', 'b'}
In above code x is deleted from my_set.

Removing not available element

This will not generate any error (KeyError)
my_set={'a','b','c','x','k'}
my_set.discard('y')

Difference between discard() and remove()

If the element is not present then discard() will not generate error, but remove() method will raise error.
my_set={'a','b','c','x','k'}
my_set.discard('y')
print(my_set)
Output
{'x', 'c', 'a', 'k', 'b'}

All set methods Questions with solutions on set discard()

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer