Categories
Biracial Dating apps

How to remove things from a list while iterating?

How to remove things from a list while iterating?

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

local teen dating site

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.