This is the most common problem I face while trying to learn programming in python. The problem is, when I try to iterate a list using “range()” function to check if given item in list meets given condition and if yes then to delete it, it will always give “IndexError”. So, is there a particular way to do this without using any other intermediate list or “while” statement? Below is an example:
l = range(20)
for i in range(0,len(l)):
if l[i] == something:
l.pop(i)
First of all, you never want to iterate over things like that in Python. Iterate over the actual objects, not the indices:
The reason for your error was that you were removing an item, so the later indices cease to exist.
Now, you can’t modify a list while you are looping over it, but that isn’t a problem. The better solution is to use a list comprehension here, to filter out the extra items.
You can also use the
filter()builtin, although that tends to be unclear in most situations (and slower where you needlambda).Also note that in Python 3.x,
range()produces a generator, not a list.It would also be a good idea to use more descriptive variable names – I’ll presume here it’s for example, but names like
iandlare hard to read and make it easier to introduce bugs.Edit:
If you wish to update the existing list in place, as pointed out in the comments, you can use the slicing syntax to replace each item of the list in turn (
l[:] = new_l). That said, I would argue that that case is pretty bad design. You don’t want one segment of code to rely on data being updated from another bit of code in that way.Edit 2:
If, for any reason, you need the indices as you loop over the items, that’s what the
enumerate()builtin is for.