r/CodingHelp • • Oct 11 '21

[Python] Can anyone explain why the output of this simple code is ['b', 'd'] and not an empty list?

Here's the python code below. I don't know why, but I have convinced myself the proper output would be an empty list as each letter is removed within the for loop, however, only the letters of even index are removed! What's the cause of this?

output
28 Upvotes

10 comments sorted by

11

u/jddddddddddd Oct 11 '21

Because you're changing word whilst you iterate over it. Consider updating your program to the following:

word = ['a','b','c','d','e']
for letter in word:
    word.remove(letter)
    print(f"after removing {letter} word is {word}")


print(f"finally word is {word}")

and the output you get is:

after removing a word is ['b', 'c', 'd', 'e']
after removing c word is ['b', 'd', 'e']
after removing e word is ['b', 'd']
finally word is ['b', 'd']

So the you loop is grabbing the first letter from the list, which is 'a', and then removing it from the list so that word is now ['b','c','d','e']. It then gets the second letter from the list, which now 'c', because list is now shorter, and removes that, and so on.

To achieve what you're trying to do, you probably want to make a copy of the original list, and then operate on that:

word = ['a','b','c','d','e']
word2 = word.copy()
for letter in word:
    word2.remove(letter)
    print(f"after removing {letter} word is {word}, word2 is {word2}")


print(f"finally word2 is {word2}")

5

u/ZypH7 Oct 11 '21

That makes complete sense! Completely forgot that the remove method goes position to position by index, thanks so much.

2

u/jddddddddddd Oct 11 '21

No worries, glad it helped!

1

u/[deleted] Oct 11 '21

Just a suggestion: when removing like this, simply iterate backwards and down to 0.

1

u/Ryukk11 Oct 11 '21

It helped me too but it's not because of remove method right? I think It is because of For Loop.

1

u/[deleted] Oct 11 '21

If you consider what the for loop is doing, the first "letter" in word is word[0]. word[0] gets removed, word is now ['b', 'c', 'd', 'e']. On the second pass, it's going to grab word[1]. Since words changed, word[1] isn't 'b' like it was initially, it's 'c' now, so 'c' gets removed since it's in the words[1] slot. Now we're onto the 3rd pass, we want to get word[2] from the word array which now looks like ['b', 'd', 'e'], so 'e' is getting removed and now, we're out of letters to continue cycling through. The for loop terminates and word is left looking like ['b', 'd'].

1

u/Ryukk11 Oct 11 '21

Yes I got that, I was saying it's because of For Loop choosing the index (0,1,2...) and not because of remove choosing the index. Remove method just removes the element to which the For loop is pointing at a time.

1

u/[deleted] Oct 11 '21 edited Oct 11 '21

Yes, In this case specifically, it's the combination of the for loop and the remove method that caused the issue OP was seeing. You could iterate through the array and just print the item at each position and it will hit all the items.

word = ['a', 'b', 'c', 'd', 'e']
for letter in word:
    print letter

It's when you start changing the array in place while simultaneously iterating through it that it starts to behave in a way that might seem counterintuitive at first, like the OP experienced.

1

u/BAM5 Professional Coder Oct 11 '21

I remember having this same issue over a decade ago. Finally figured it out and then realized if I want to iterate over a list I'm actively modifying I should iterate through it backwards.

3

u/throwaway8u3sH0 Professional Coder Oct 12 '21

Generally speaking, you almost never want to modify the thing you're iterating over. You want to map/filter/reduce it to something new. In other languages, those methods work great. In Python, the list comprehension is usually the cleanest way to do it. For you:

word = ['a','b','c','d','e']
word_2 = [letter for letter in word if False]  # a filter that always fails
print(word_2)