How to remove an element from a list by index in Python?
I found the list.remove method, but say I want to remove the last element, how do I do this? It seems like the default remove searches the list, but I don't want any search to be performed.
|
Use
Here is the section from the tutorial. |
|||||||||||||||||||||
|
|
You probably want
By default,
|
|||||||||||||||||||||
|
|
Like others mentioned pop and del are the efficient ways to remove an item of given index. Yet just for the sake of completion ( since the same thing can be done via many ways in python ): Using slices ( This does not do inplace removal of item from original list ) : ( Also this will be the least efficient method when working with python list but this could be useful ( but not efficient, I reiterate ) when working with user defined objects that do not support pop, yet do define a
Note: Please note that this method does not modify the list inplace like This makes this method very inefficient and it can also produce undesirable side effects ( especially when other variables point to the original list object which remains un-modified ) Thanks to @MarkDickinson for pointing this out ... This Stack Overflow answer explains the concept of slicing. Also note that this works only with positive indices. While using with objects, the In essence this works with any object whose class definition is like :
This works with Comparison of the three ways in terms of efficiency: Assume the following is predefined :
The By far the most efficient method. Works will all objects that define a The disassembly is as follows : Code:
Disassembly:
Less efficient than the del method. Used when you need to get the deleted item. Code:
Disassembly:
The slice and add method. The least efficient. Code:
Disassembly:
Note : In all three disassembles ignore the last 2 lines which basically are |
|||||||||||||||||||||
|
|
|
|||||||||||||
|
|
Generally, I am using the following method:
|
|||
|
|
|
As previously mentioned, best practice is del(); or pop() if you need to know the value. An alternate solution is to re-stack only those elements you want:
eta: hmm... will not work on negative index values, will ponder and update I suppose
would patch it... but suddenly this idea seems very brittle. Interesting thought experiment though. Seems there should be a 'proper' way to do this with append() / list comprehension. pondering |
||||
|
|
|
One can either use del or pop, but I prefer del, since you can specify index and slices, giving the user more control over the data. |
|||
|
|
|
You can use either del or pop to remove element from list based on index. Pop will print member it is removing from list, while list delete that member without printing it.
|
|||
|
|
|
It doesn't sound like you're working with a list of lists, so I'll keep this short. You want to use pop since it will remove elements not elements that are lists, you should use del for that. To call the last element in python it's "-1"
|
|||
|
|
O(n)in time operation.deque()provides efficient operations on both ends but it does not provide O(1) insertions/lookups/deletions in the middle. – J.F. Sebastian Sep 8 '15 at 0:32