I am currently studying python for job interview purposes. I am trying to wrap my head around the following regarding lists in python:
why do the following two pieces of code result in different outputs?
arr = [1,2,3,9,10,12]
for i in arr:
arr.remove(i)
This results in the list [2,9,12] however the following:
arr = [1,2,3,9,10,12]
for i in list(arr):
arr.remove(i)
This results in the desired empty list []. Can anyone explain why this is? Shouldn't iterating the list in the first manner also result in an empty list since remove() removes by item value?
Disclaimer, I am testing this all out using Python3 via HackerRank.