Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP!!!
#1
#######################################
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??
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Aug-06-2020, 04:15 PM)buran Wrote: Never change list while iterating over it. Iterate over copy of the list

Habibi, Thank you
Reply
#4
Or create a new list containing only the values you want to keep, using a list comprehension or filter.
Reply
#5
(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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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 []
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020