Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

python - Best way to remove elements from a list

I would like to know what is the best way/efficient way to remove element(s) from the list.

There are few functions provided by Python:

  1. some_list.remove(value), but it throws error if value is not found.
  2. some_list.pop(some_list[index]), removes the item at the given position in the list, and return it.
  3. del (some_list[index]), it removes element from the given index, it's different from pop as it doesn't return value.

Scenarios:

  • If you have few items to remove say one element or between 1 to 5.
  • If you have to remove multiple items in a sequence.
  • If you have to remove different items based on a condition.
  • How about if you have a list of lists and want to remove elements in sequence.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

My answer is not exactly to your question but after you read this, I hope you can decide which type you need to choose for your needs.

Python’s lists are variable-length arrays, not Lisp-style linked lists. The implementation uses a contiguous array of references to other objects, and keeps a pointer to this array.

This makes indexing a list a[i] an operation whose cost is independent of the size of the list or the value of the index.

When items are appended or inserted, the array of references is resized. Some algorithm is applied to improve the performance of appending items repeatedly; when the array must be grown, some extra space is allocated so the next few times don’t require an actual resize i.e over-allocation. More Information

Removing vs Pop vs Delete:

At first glance it looks like all of them are doing the same thing.

Under the hood its behaving different.

removing : remove an element from the list by iterating from 0 index till the first match of the element is found. taking more time to iterate if the element is at the end.

pop : removing element from the list by using the index. taking less time.

del : is a python statement that removes a name from a namespace, or an item from a dictionary, or an item from a list by using the index.

REMOVE:

  • it removes the first occurence of value.
  • raises ValueError if the value is not present.
  • it takes only one argument, so you can't remove multiple value in one shot.

POP:

  • remove and return item at index (default last).
  • Raises IndexError if list is empty or index is out of range.
  • it takes only one argument, so you can't remove multiple value in one shot.

DEL:

  • remove the item at index and return nothing.
  • it can remove slices from a list or can clear the whole list.

Benchmark:

Worst case : deleting from the end of the list.

yopy:-> python -m timeit "x=range(1000)" "x.pop(999)"
100000 loops, best of 3: 10 usec per loop
yopy:-> python -m timeit "x=range(1000)" "x.remove(999)"
10000 loops, best of 3: 31.3 usec per loop
yopy:-> python -m timeit "x=range(1000)" "del x[999]"
100000 loops, best of 3: 9.86 usec per loop
yopy:->

Best case: begining of the list.

yopy:-> python -m timeit "x=range(1000)" "x.remove(1)"
100000 loops, best of 3: 10.3 usec per loop
yopy:-> python -m timeit "x=range(1000)" "x.pop(1)"
100000 loops, best of 3: 10.4 usec per loop
yopy:-> python -m timeit "x=range(1000)" "del x[1]"
100000 loops, best of 3: 10.4 usec per loop
yopy:->

Point to be noted:

if array grows or shrinks in the middle

  • Realloc still depends on total length.
  • But, All the trailing elements have to be copied

So, now I hope you can decide what you need to choose for your needs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...