Removing a particular index from a list

S

Srikanth

Hi,

list.remove(item) removes the first item from the list, but how do I
say to remove a particular index from a list without using it's value?

Let's say I have 4 items in my list as
li = ["sri", "s", "srikanth", "s"]

And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it. This is my problem.

Thanks,
Srikanth
 
S

Steve Holden

Srikanth said:
Hi,

list.remove(item) removes the first item from the list, but how do I
say to remove a particular index from a list without using it's value?

Let's say I have 4 items in my list as
li = ["sri", "s", "srikanth", "s"]

And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it. This is my problem.
>>> li = ["sri", "s", "srikanth", "s"]
>>> del li[-1]
>>> li ['sri', 's', 'srikanth']
>>>

regards
Steve
 
C

Cyril Cheneson

li = ["sri", "s", "srikanth", "s"]
And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it. This is my problem.

Thanks,
Srikanth

li.pop() will remove the last element

Cyril
 
P

Peter Otten

Srikanth said:
list.remove(item) removes the first item from the list, but how do I
say to remove a particular index from a list without using it's value?

Let's say I have 4 items in my list as
li = ["sri", "s", "srikanth", "s"]

And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it.

del li[-1]
This is my problem.

This, or not reading the tutorial.

http://docs.python.org/tut/node7.html#SECTION007200000000000000000

Peter
 
S

Steve Holden

Peter said:
Srikanth said:
list.remove(item) removes the first item from the list, but how do I
say to remove a particular index from a list without using it's value?

Let's say I have 4 items in my list as
li = ["sri", "s", "srikanth", "s"]
And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it.

del li[-1]
This is my problem.

This, or not reading the tutorial.
No, that's OUR problem :)
 
P

Peter Otten

Steve said:
Peter said:
Srikanth said:
list.remove(item) removes the first item from the list, but how do I
say to remove a particular index from a list without using it's value?

Let's say I have 4 items in my list as

li = ["sri", "s", "srikanth", "s"]
And if I want to remove the last item ("s"), how do I remove it?
li.remove(-1) doesn't do it.

del li[-1]
This is my problem.

This, or not reading the tutorial.
No, that's OUR problem :)

And the fact that it's our /self-inflicted/ problem makes it all the more
severe :)

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top