-1

I have code:

listA = [i for i in range(5)]

for x in listA :
    print(listA)
    print('prepare to remove %s'%x)
    listA.remove(x)
    listA = [i for i in range(x, 10)]
    print(listA)

I guess x in for loop will be: 0 1 2 3 4

but the result is 0 2 3 4

Why ? This is so weird...

I just want to know why the 1 are disappeared ,Why not print 0 2 4 or 01234 ? Just 1

someone can explain this?

Vic
  • 570
  • 2
  • 13
  • 28

4 Answers4

1

refer code below

  • during 1st iteration, lst.remove(x) effectively remove 1 item from lst with id xxxx, lst is now [1,2,3,4]
  • at 2nd iteration, since 0 already being remove, it will get 2 at index 1 when trying to get next item
  • from 2nd iteration onwards, like @Harlekuin mention, you are performing remove operation on a totally new created list, which won't impact original list with id xxxx, folowing iteration will continue on lst with id xxxx [1,2,3,4], so its 3 following by 4

code

lst = [0, 1, 2, 3, 4] # define raw list to make it clearer
# id(lst) = xxxx

# iterate over lst with id xxxx
for x in lst: 
    print('prepare to remove %s'%x)
    lst.remove(x) # 1st loop operate on list with id xxxx, 2nd loop onwards operate on list with id yyyy
    lst = [i for i in range(x, 10)] # lst being assign to another id, lets assume its yyyy now
    # id(lst) = yyyy
Skycc
  • 3,405
  • 1
  • 9
  • 17
0

It's because you remove x (being an integer), and then recreate the list with range(x, 10). The first time you do this it will remove the 1 from the original list, but the list is recreated before printing, so only the first loop will lose a number. In other words,

list.remove(x)

is useless once you:

list = [i for i in range(x, 10)]
Tom Malkin
  • 1,904
  • 2
  • 17
  • 34
0

In first loop you remove 0 from listA which refers to oryginal [0, 1, 2, 3, 4] and you have [1, 2, 3, 4]. for uses reference to the same list so in next loop it use [1, 2, 3, 4] and it skips 1.

But meanwhile you assign new list to variable listA so in next loop you remove 1 from new list - but for still use reference to oryginal [1, 2, 3, 4] and it doesn't skip other elements.

furas
  • 119,752
  • 10
  • 94
  • 135
0

The problem is that you use the same list in your loop condition and inside.

# List to loop x over
listB = [i for i in range(5)]
# List to manipulate with remove
listA = [i for i in range(0, 10)]
for x in listB:
    print('loop: %s'%x)
    print('list before removing %s'%x)
    print(listA)
    listA.remove(x)
    print('list after removing %s'%x)
    print(listA)
    # Re-initialize listA
    listA = [i for i in range(x, 10)]

This produces the following output:

loop: 0
list before removing 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list after removing 0
[1, 2, 3, 4, 5, 6, 7, 8, 9]
loop: 1
list before removing 1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list after removing 1
[0, 2, 3, 4, 5, 6, 7, 8, 9]
loop: 2
list before removing 2
[1, 2, 3, 4, 5, 6, 7, 8, 9]
list after removing 2
[1, 3, 4, 5, 6, 7, 8, 9]
loop: 3
list before removing 3
[2, 3, 4, 5, 6, 7, 8, 9]
list after removing 3
[2, 4, 5, 6, 7, 8, 9]
loop: 4
list before removing 4
[3, 4, 5, 6, 7, 8, 9]
list after removing 4
[3, 5, 6, 7, 8, 9]
Gyebro
  • 1,511
  • 13
  • 19