0

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.

3 Answers 3

3
for i in arr:
   ...

Internally uses indexes to remove elements from the array, meaning that if the first element is removed, it continues to index 2, which is the 3, it removes the 3, then goes to index 4 and then it continues ...

The second one however does not do this and actually goes over each value and removes it because the array doesn't resize during removing

Sign up to request clarification or add additional context in comments.

Comments

1

The iterator first points to position 0 and then the first entry 2 is removed. In the second iteration however it's pointing to position 1 but your list becomes [2,3,9,10,12] and therefore it's removing 3 (the element in position 1). So the first element of the list never gets removed, and for the same reason, as the iterator moves on, it skips every other element. In the second example, list(arr) creates a new copy and since it's never modified during iteration all its elements are pointed to and then removed from the original list.

In general it's not good to be removing elements from a collection while iterating over it.

Comments

0

Try taking a look at the following code. It should help you understand what is taking place behind the scenes:

arr = [1,2,3,9,10,12]
index = 0

for i in arr:
  print('First print: ', i, index, arr[index])
  arr.remove(i)
  print('Second print: ', i, index, arr[index])
  index += 1

  # ('First print: ', 1, 0, 1)
  # ('Second print: ', 1, 0, 2)
  # ('First print: ', 3, 1, 3)
  # ('Second print: ', 3, 1, 9)
  # ('First print: ', 10, 2, 10)
  # ('Second print: ', 10, 2, 12)

arr = [1,2,3,9,10,12]
index = 0

for i in list(arr):
  print('First print: ', i, index, arr[index])
  arr.remove(i)
  print('Second print: ', i, index, arr[index])
  index += 1

  # ('First print: ', 1, 0, 1)
  # ('Second print: ', 1, 0, 2)
  # ('First print: ', 2, 1, 3)
  # ('Second print: ', 2, 1, 9)
  # ('First print: ', 3, 2, 10)
  # ('Second print: ', 3, 2, 12)
  # Traceback (most recent call last):
  #  File "<stdin>", line 2, in <module>
  # IndexError: list index out of range

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.