How to remove an element from a list by index in Python?

PythonServer Side ProgrammingProgramming

There are two options to remove an element by its index in list. Using del statement, and using pop() method.

The del statement needs index of the element to remove.

>>> L1=[11,22,33,44,55,66,77]
>>> del L1[2]
>>> L1
[11, 22, 44, 55, 66, 77]

The pop() method of built-in list class requires index as argument. The method returns the removed elements and reduces contents of list by one element.

>>> L1=[11,22,33,44,55,66,77]
>>> x=L1.pop(2)
>>> x
33
>>> L1
[11, 22, 44, 55, 66, 77]


raja
Published on 12-Jan-2018 19:38:51
Advertisements