4

Is there a way so that I could give a position in a list, e.g. 2 for the third value, and then directly use that position, e.g list.remove[2]?

For example:

Say my list was,

test = [0,1,2,3,2,2,3]

Is there a way so that if the user wanted to remove the 5th value, which is the middle 2, then you could just give a position and use it directly. In this case, if I was to use test.remove(2) it would remove the first 2 that appeared in the list, so is there a way that I could say the middle 2's position and then remove it without touching the rest of the list?

Something like test.remove[2] would be a good idea to implement, the value in the [] being the position.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • you can see: http://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists – sinceq Feb 18 '14 at 03:27

3 Answers3

2

You can use list.pop:

>>> test = [0,1,2,3,2,2,3]
>>> test.pop(5)            #Removes the item and returns it as well
2
>>> test
[0, 1, 2, 3, 2, 3]

or del:

>>> test = [0,1,2,3,2,2,3]
>>> del test[5]           #Only removes the item
>>> test
[0, 1, 2, 3, 2, 3]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2

Sometime early on in my use of this resource someone suggested I learn how to use Python's introspection ability as that would often help answer my questions. In your case

test =  [0,1,2,3,2,2,3]

To learn about the methods that test has I can do

dir(test)

The results are

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Ignoring the items that begin with an underscore and using some language skills I want to understand whether pop or remove will help me so

help(test.pop)

Help on built-in function pop:

pop(...)
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.

Note this is not a criticism of your question rather I hope this helps you understand that Python has some tools to help you understand features very efficiently.

PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
  • +1, great suggestion. The web help is also nice. Once you find [the right section](http://docs.python.org/3/library/stdtypes.html#mutable-sequence-types), it's all there, and formatted more nicely than in your interactive interpreter. Unfortunately, it's a known problem that searching for methods on the built-in types (whether via Google or the docs' own Quick Search) does not work nearly as well as for things like top-level functions, methods on stdlib module types, etc. Eventually you'll get used to finding this stuff, but it will sadly take a lot of fumbling around at first. – abarnert Feb 18 '14 at 03:52
  • Thanks! Pop has helped me, I'm still studying Python in school so I'm kinda new to it, so any help is nice. Also, I've already been looking on the web, but instead of finding whats AT a position and then using it, it keeps telling me how to find the position. –  Feb 18 '14 at 10:35
2

The remove method exists specifically for searching by value. When you already know the index, you don't need it; just index the list directly.

To get the value by index, you don't use find, or otherwise search the list looking for a value that matches, you just do this:

value = test[2]

To replace a value by index, you don't use replace, or otherwise search the list, you just do this:

test[2] = new_value

And to delete a value by index, you don't use remove, or otherwise search the list, you just do this:

del test[2]

This is explained in the tutorial section on The del statement, but you really probably want to read that entire chapter on lists.

abarnert
  • 354,177
  • 51
  • 601
  • 671