How to delete an item in a list if it exists?

前端 未结 7 934
無奈伤痛
無奈伤痛 2020-11-29 14:55

I am getting new_tag from a form text field with self.response.get(\"new_tag\") and selected_tags from checkbox fields with



        
7条回答
  •  一生所求
    2020-11-29 15:06

    Adding this answer for completeness, though it's only usable under certain conditions.

    If you have very large lists, removing from the end of the list avoids CPython internals having to memmove, for situations where you can re-order the list. It gives a performance gain to remove from the end of the list, since it won't need to memmove every item after the one your removing - back one step (1).
    For one-off removals the performance difference may be acceptable, but if you have a large list and need to remove many items - you will likely notice a performance hit.

    Although admittedly, in these cases, doing a full list search is likely to be a performance bottleneck too, unless items are mostly at the front of the list.

    This method can be used for more efficient removal,
    as long as re-ordering the list is acceptable. (2)

    def remove_unordered(ls, item):
        i = ls.index(item)
        ls[-1], ls[i] = ls[i], ls[-1]
        ls.pop()
    

    You may want to avoid raising an error when the item isn't in the list.

    def remove_unordered_test(ls, item):
        try:
            i = ls.index(item)
        except ValueError:
            return False
        ls[-1], ls[i] = ls[i], ls[-1]
        ls.pop()
        return True
    

    1. While I tested this with CPython, its quite likely most/all other Python implementations use an array to store lists internally. So unless they use a sophisticated data structure designed for efficient list re-sizing, they likely have the same performance characteristic.

    A simple way to test this, compare the speed difference from removing from the front of the list with removing the last element:

    python -m timeit 'a = [0] * 100000' 'while a: a.remove(0)'
    

    With:

    python -m timeit 'a = [0] * 100000' 'while a: a.pop()'
    

    (gives an order of magnitude speed difference where the second example is faster with CPython and PyPy).

    1. In this case you might consider using a set, especially if the list isn't meant to store duplicates.
      In practice though you may need to store mutable data which can't be added to a set. Also check on btree's if the data can be ordered.

提交回复
热议问题