Python list remove() method

The remove() list method purpose is to remove an element from the list object.The element can be integer real number or a sequence object string or list or tuple.

>>> ls=[ 23 , 'New' , [23 ,56 , "Text"] , [23] , 78.35 , 'New' , (23)]
>>> ls.remove('New')
>>> ls
[23, [23, 56, 'Text'], [23], 78.35, 'New', 23] #'New' string is removed
>>> ls.remove([23, 56, 'Text'])
>>> ls
[23, [23], 78.35, 'New', 23] #the tuple [23, 56, 'Text'] is removed

If the element to be removed is not found in the list object you get an error,consider the code below.

>>> ls=[23, [23], 78.35, 'New', 23]
>>> ls.remove( 1000 )
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    ls.remove(1000)
ValueError: list.remove(x): x not in list

You can see the error message ‘list.remove(x): x not in list’.

Point to note

If there are two or more similar elements in the list object the first one is removed.

>>> ls=[ '1' , 1 , 'Hap' , ( 'Too',) , 1 , 34 ]
>>> ls.remove(1)
>>> ls
['1', 'Hap', ('Too',), 1, 34]

Calling ls.remove(1) removes the first 1 the second one is left untouched.

Similar method pop().