30 of 151 menu

The discard method

The discard method removes elements that are in the set. If there is no element, then the original set will simply be returned. Unlike the remove method, which returns an error. In the method parameter, we pass the element we want to remove.

Syntax

set.discard(what to delete)

Example

Let's remove the element 'a' from our set:

st = {'a', 'b', 'c'} st.discard('a') print(st)

Result of code execution:

{'b', 'c'}

Example

Now let's remove an element that is not in the set:

st = {'a', 'b', 'c'} st.discard('e') print(st)

After executing the code, we will get back the original array:

{'a', 'b', 'c'}

See also

  • method remove,
    which removes elements from a set
  • method pop,
    which removes the first element from a set
  • method clear,
    which clears the multitude
  • method add,
    which adds elements to a set
  • function len,
    which returns the length of the set
  • method union,
    which unites sets
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline