3

I tried to remove only one element from an array and print remaining ones in a loop:

arr = [1,2,3,4,5]
for i in arr:
    a = arr
    a.remove(i)
    print a

So I am expecting it to print this:

[2, 3, 4, 5]
[1, 3, 4, 5]
[1, 2, 3, 5]
[1, 2, 3, 4]

Why am I gettting the following results instead:

[2, 3, 4, 5]
[2, 4, 5]
[2, 4]
3
  • a = arr does not create a copy of arr - presumably you just need a = arr[:] Commented Jun 29, 2017 at 5:49
  • 1
    Possible duplicate of Remove items from a list while iterating Commented Jun 29, 2017 at 5:52
  • 1
    I'm not so sure this is a duplicate, because this question is asking how to temporarily remove an element from an array (which, in practice, means not truly removing the element) whereas the other one is asking how to permanently remove an element. Commented Jun 29, 2017 at 5:56

4 Answers 4

4

This is a classic problem of deep vs shallow copy. Python copies the array by reference. So, any changes to the new variable (a in your case) will be reflected in the main array (arr). Removing element from a also remove elements from arr. You need to use following for creating a copy.

 a = arr[:] 

This will not remove any elements from arr.

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

1 Comment

Not so sure about the deep copy part. The items are integers so they are immutable. A shallow, std, copy would probably have the same effect, since we are in this case really only concerned about the container object. Note: when I talk about deep, I am referring to term as used in the copy module. Yours is a shallow copy - if it was containing lists for example, you'd still be modifying the same items after it.
0

You've already got explanations, I'll provide an alternative - you can produce requested output without using remove at all:

arr = [1,2,3,4,5]
for i in arr:
    a = [x for x in arr if x != i]
    print a

(beware of non-unique elements in arr, if there are any, both this code and yours will produce unexpected results, though in different ways).

Comments

0

You can try this.

arr = [1, 2, 3, 4, 5, 6, 7 ]
for index, item in enumerate(arr):
    print(arr[0:index] + arr[index+1:])

I think this can help you.

If you don't have any repeated value you can use this too.

for item in arr:
   print(set(arr) - set([item]))

Comments

0

Python lists are mutable i.e. modifying operations (like list.remove()) on them do not produce a new list but modify the existing list instead so each your cycle actually permanently modifies the source list and elements are lost. You'd need to copy your whole list each time you want to modify it in order to achieve what you want, or you can create a new list with elements excluded, or, for very long lists, reconstructing by slicing is probably the most performant way:

arr = [1,2,3,4,5]
for i in range(len(arr)):
    a = arr[0:i] + arr[i+1:]
    print(a)

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.