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]
sprogissd
  • 2,755
  • 5
  • 24
  • 45
  • `a = arr` does not create a copy of `arr` - presumably you just need `a = arr[:]` – AChampion Jun 29 '17 at 05:49
  • 1
    Possible duplicate of [Remove items from a list while iterating](https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) – eyllanesc Jun 29 '17 at 05: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. – David Z Jun 29 '17 at 05:56

4 Answers4

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.

raks
  • 329
  • 1
  • 5
  • 11
  • 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. – JL Peyret Jun 29 '17 at 06:03
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).

Błotosmętek
  • 12,717
  • 19
  • 29
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]))
Govind
  • 434
  • 4
  • 8
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)
zwer
  • 24,943
  • 3
  • 48
  • 66