29 of 151 menu

The remove method

The remove method removes elements from a set. If the element is not in the set, an error will be returned. In the method parameter, we pass the element we want to remove.

Syntax

set.remove(what we remove)

Example

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

st = {'a', 'b', 'c'} st.remove('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.remove('e') print(st)

After executing the code, we will get an error:

KeyError: 'e'

See also

  • method discard,
    which removes elements that are in the 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