Python Forum

Full Version: HELP!!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#######################################
number = [1,1,1,1,1,1,1,1,1,1]

for i in car:
    if(i == 1):
        number.remove(i)

print(car)
#######################################

The above code gives following result:

Result:[1, 1, 1, 1, 1]

Should'nt it give empty list??
Never change list while iterating over it. Iterate over copy of the list.


EDIT: Although correct statement, my answer does not address OP problem. look at @snippsat post below.
(Aug-06-2020, 04:15 PM)buran Wrote: [ -> ]Never change list while iterating over it. Iterate over copy of the list

Habibi, Thank you
Or create a new list containing only the values you want to keep, using a list comprehension or filter.
(Aug-06-2020, 04:13 PM)munirh070 Wrote: [ -> ]#######################################
number = [1,1,1,1,1,1,1,1,1,1]

for i in car:
    if(i == 1):
        number.remove(i)

print(car)
#######################################

The above code gives following result:

Result:[1, 1, 1, 1, 1]

Should'nt it give empty list??

It should give
Error:
Traceback (most recent call last): File "c:/Users/Dave/Documents/VS Code WorkSpaces/pythonforum/general/forumpost.py", line 3, in <module> for i in car: NameError: name 'car' is not defined
Wall Wall Wall
I was on a phone when I replied. Completely overlooked that. Don't know how I get confused OP is iterating over same list... Angel
I find this example informative:
x = [n for n in range(1, 11)]
print('Before', x)
for i in x:
    x.remove(i)
print('After', x)
Output:
Before [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After [2, 4, 6, 8, 10]
Why is it removing every other number? It all makes sense if you rewrite the program like this:
x = [n for n in range(1, 11)]
print('Before', x)
i = 0
while i < len(x):
    value = x[i]
    x.remove(value)
    print(i, value, x)
    i += 1
print('After', x)
Output:
Before [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 0 1 [2, 3, 4, 5, 6, 7, 8, 9, 10] 1 3 [2, 4, 5, 6, 7, 8, 9, 10] 2 5 [2, 4, 6, 7, 8, 9, 10] 3 7 [2, 4, 6, 8, 9, 10] 4 9 [2, 4, 6, 8, 10] After [2, 4, 6, 8, 10]
The first time through the loop the index is 0, and x[0] is removed from x. x is now one element shorter and all the values are shifted to the left.
The second time through the loop the index is 1. x[1] is 3, not 2. By removing a value from the list we changed the relationship between index and values, but the loop ignores this.

This same thing happened when I used for i in x. The for loop did not make a copy of the list and use that to iterate. The for loop must be using an index and an increment, just like the while loop example.

An easy solution is to use a copy of the list and use that to iterate:
x = [n for n in range(1, 11)]
print('Before', x)
i = 0
for i in x[:]:
    x.remove(i)
print('After', x)
Output:
Before [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After []