Criminal Minds Beyond Borders 25 | Droga bez powrotu | Read More

, asked
Views : 3

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

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

1 Answer
Malhar Lathkar

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]

Advertisements
Related Questions