How to iterate over a list when removing items from a list using the range () function?

This is the most common problem I am facing when trying to learn python programming. The problem is that when I try to iterate over the list using the "range ()" function to check if the given element in the list matches the specified condition, and if so, it will always indicate "IndexError" to delete it. So, is there a way to do this without using any other intermediate list or while statement? The following is an example:

l = range(20)
for i in range(0,len(l)):
  if l[i] == something:
    l.pop(i)
+3
source share
6 answers

First of all, you never want to iterate over such things in Python. Iterations over actual objects, not indexes:

l = range(20)
for i in l:
    ...

, , .

, , . , , .

l = range(20)
new_l = [i for i in l if not i == something]

filter() , ( , lambda).

, Python 3.x range() , .

- , , , i l, .

Edit:

, , (l[:] = new_l). , , - . , , .

2:

- , , enumerate() builtin .

+7

:

newlist=[i for i in oldlist if not condition ]
+2

: , "-". :

l = [i for i in xrange(20) if i != something]
0
  • for i in range(0,len(l)):, for i, item in enumerate(l): , , for item in l: if not
  • , . ,
  • l ( 1 I)
  • , . filter()

, :

while something in list_: list_.remove(something)

. .

0

, , .

.

l[:] = [item for item in l if item != something]
0

, IndexError, , , for. , ...

#-- Build the original list: [0, 1, 2, ..., 19]
l = range(20)

#-- Here, the range function builds ANOTHER list, in this case also [0, 1, 2, ..., 19]
#-- the variable "i" will be bound to each element of this list, so i = 0 (loop), then i = 1 (loop), i = 2, etc.
for i in range(0,len(l)):
    if i == something:
        #-- So, when i is equivalent to something, you "pop" the list, l.
        #-- the length of l is now *19* elements, NOT 20 (you just removed one)
        l.pop(i)
    #-- So...when the list has been shortened to 19 elements...
    #-- we're still iterating, i = 17 (loop), i = 18 (loop), i = 19 *CRASH*
    #-- There is no 19th element of l, as l (after you popped out an element) only
    #-- has indices 0, ..., 18, now.

, "" , , . - ? ...

if l[i] == something:
    l.pop(i)

(l[i] == i), .

, , . ( : , , , , zip ..)

...

#-- Create a function for testing the elements of the list.
def f(x):
    if (x == SOMETHING):
        return False
    else:
        return True

#-- Create the original list.
l = range(20)

#-- Apply the function f to each element of l.
#-- Where f(l[i]) is True, the element l[i] is kept and will be in the new list, m.
#-- Where f(l[i]) is False, the element l[i] is passed over and will NOT appear in m.
m = filter(f, l)

"", Python . ...

#-- Create the original list.
l = range(20)

#-- Apply the function f to each element of l.
#-- Where lambda is True, the element l[i] is kept and will be in the new list, m.
#-- Where lambda is False, the element l[i] is passed over and will NOT appear in m.
m = filter(lambda x: (x != SOMETHING), l)

, !

0

All Articles