I’m iterating over a list of tuples in Python, and have always been trying to take them off if they meet certain criteria.
Exactly What must I use in place of code_to_remove_tup ? I cannot figure out how to take away the item in this manner.
25 Responses 25
You can make use of an inventory comprehension to create a list that is new just the elements you never want to eliminate
Or, by assigning to your piece somelist[ ] , you can mutate the list that is existing contain just the items you would like
This process could possibly be helpful if there are more references to somelist that want to mirror the changes.
In the place of a comprehension, you could also use itert ls . In Python 2
The answers suggesting list comprehensions are ALMOST proper — except it the same name the old list as, they do NOT modify the old list in place that they build a completely new list and then give. That’s not the same as that which you’d be doing by selective treatment, as in @Lennart’s suggestion — it’s faster, but if your list is accessed via multiple recommendations the fact you’re just reseating among the recommendations and never altering the list object itself can cause simple, disastrous pests.
Happily, it is incredibly very easy to get both the speed of list comprehensions AND the needed semantics of in-place alteration — simply rule
Note the subtle distinction with other responses this one is NOT assigning to a barename – it’s assigning to a list piece that just happens to be the entire list, thus changing the list contents in the exact same Python list object, instead of just reseating one reference (from past list object to new list object) like the other answers.
You will need to take a content for the list and iterate over it first, or the iteration will fail in what may be unanticipated outcomes.
For example (depends on which kind of list)
You’ll want to go backwards otherwise it’s a bit like sawing off the tree-branch you are sitting on -)
Python 2 users replace range by xrange to avoid creating a hardcoded list
Breakdown of workaround
make use of connected list implementation/roll your very own.
A connected list is the proper data structure to guide efficient item removal, and doesn’t force one to make space/time tradeoffs.
A CPython list is implemented with powerful arrays as stated here, that will be not a g d data type to aid removals.
There doesn’t seem to be a connected list within the library that is standard
begin a brand new list() from scratch, and .append() Back at the final end as mentioned at https //stackoverflow /a/1207460/895245
This time around efficient, but less area efficient because it keeps a extra copy of the array around during iteration.
This might be more r m efficient it is less time efficient, because removal from dynamic arrays requires shifting all following items back by one, which is O(N) since it dispenses the array copy, but.
Generally, it quick and dirty and do not desire to add a customized LinkedList class, https://hausoftaeyong.me/wp-content/uploads/2020/06/EZumu8sU0AAqVEP.jpeg” alt=”escort services in Birmingham”> you need to opt for the quicker .append( if you should be doing) option by standard unless memory is really a big concern.
Certified Python 2 tutorial 4.2. “for Statements”
This part of the docs makes it clear that
- you’ll want to produce a content for the iterated list to change it
- one way to do so has been the slice notation [ ]
That you first make a copy if you need to modify the sequence you are iterating over while inside the l p (for example to duplicate selected items), it is recommended. Iterating over a series does not implicitly create a content. The slice notation makes this particularly convenient
Python 2 documentation 7.3. “The for statement”
This part of the docs says yet again that you must make a content, and provides a removal example that is actual
Note there exists a subtlety when the series is being modified by the l p ( this can just occur for mutable sequences, i.e. lists). a interior countertop is utilized to keep tabs on which item can be used next, and also this is incremented for each iteration. Whenever this counter has already reached the sequence the cycle terminates. Which means if the suite deletes the present ( or even a previous) item through the sequence, the next item will be skipped ( since it gets the index of this current item which has been already treated). Likewise, if the suite inserts an item into the series ahead of the item that is current the existing product is going to be addressed again the next time through the cycle. This might trigger nasty pests which can be avoided by creating a temporary copy using a piece of this whole series, e.g.,
Nonetheless, we disagree with this particular execution, since .remove() needs to iterate the entire list to get the value.
Could Python do this better?