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

Use del and specify the element you want to delete with the index.

Use del and specify the element you want to delete with the index: In 9: a = range(10) In 10: a Out10: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 In 11: del a-1 In 12: a Out12: 0, 1, 2, 3, 4, 5, 6, 7, 8 Here is the section from the tutorial.

– Joan Venge Mar 9 '09 at 18:34 1 del is overloaded. For example del a deletes the whole list – Brian R. Bondy Mar 9 '09 at 18:36 another example del a2:4, deletes elements 2 and 3 – Brian R.

Bondy Mar 9 '09 at 18:37 8 pop() returns the element you want to remove. Del just deletes is. – unbeknown Mar 9 '09 at 19:14.

You probably want pop: a = 'a', 'b', 'c', 'd' a. Pop(1) # now a is 'a', 'c', 'd' By default, pop without any arguments removes the last item: a = 'a', 'b', 'c', 'd' a.pop() # now a is 'a', 'b', 'c'.

2 Don't forget pop(-1). Yes, it's the default, but I prefer it so I don't have to remember which end pop uses by default. – S.

Lott Mar 9 '09 at 18:43 Good point... that does increase readability. – Jarret Hardie Mar 9 '09 at 19:19.

A quick warning: be wary of modifying a list and iterating over it at the same time. E.g. Can you predict the outcome of this code?

Lst = range(10) for i, x in enumerate(lst): del lsti print lst.

6 Um. Your answer is not an answer to the original question. If you feel the advice is needed, include an actual answer.

– tzot Mar 10 '09 at 0:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions