Help me understand the .remove method?

I wanted to use the .remove method to solve a challenge in the python 3 course but I don’t understand the behavior of the method?
It is oddly only removing every other element in the list?
Can someone explain why that is?

def testing_lst(lst): for i in lst: lst.remove(i) return(lst) print(testing_lst([1,2,3,4,5,6]))
1 Like

If you want to remove every single item of a list you just can use the function .clear():

def testing_lst(lst):

  lst.clear()

  return lst

print(testing_lst([1,2,3,4,5,6]))

Output: [ ]

1 Like

Hi.
You’re looping on a list that you’re changing.
So, for the first loop;
lst = 1,2,3,4,5,6
i = 1
it removes 1 from the list, which becomes 2,3,4,5,6
So, when it loops back round i takes the value of the second element in the list - which is now 3.

hope that helps

Brilliant, thanks! Makes perfect sense :wink: