15 of 151 menu

The remove method

The remove method removes the specified element from the list. The method parameter specifies the element we need to remove. The method modifies the list itself. The result returned is None. Trying to remove a non-existent element will result in an exception being thrown.

Syntax

list.remove(what we want to remove)

Example

Let's remove the element 'ef' from our list using the remove method:

lst = ['ab', 'cd', 'ef', 'gh'] lst.remove('ef') print(lst)

Result of code execution:

['ab', 'cd', 'gh']

Example

Now let's try to remove an element that is not in the list:

lst = ['ab', 'cd', 'ef', 'gh'] lst.remove('12') print(lst)

Result of code execution:

ValueError: list.remove(x): x not in list

See also

  • method clear,
    which removes all elements of a list
  • method pop,
    which removes an element by its index
  • method append,
    which adds an element to the end of a list
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