1

In my current code of nested while loops, I have a statement to reload the tempSequence variable equal to the original sequence variable. However, through print-checking, you can see del(tempSequence[i]) is removing the value from both the tempSquence and sequence variables. I don't understand why the parent sequence is being affected in this way.

I have tried storing the sequence variable within the beginning of the outer loop, adding a new list inside the loop to store the original data, as well as moving the tempSequence = sequence statement multiple places throughout the code.

The easy way --> Repl.it link

The other way:

while i <= len(sequence):

    tempSequence = sequence **<<<< this doesn't work but**

    del(tempSequence[i])
    print(sequence)
    print(tempSequence)

    j = 0  **<<<< This works every loop**

The expected result would be for tempSequence to be reset equal to the original array. Then altered and iterated.

The actual result leaves the array short one value each iteration, ending with a too short range to complete all iterations

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • 1
    Simply assigning lists in Python creates a shallow copy, wherein changes are reflected in the original list as well. You should [deep copy](https://stackoverflow.com/questions/17873384/how-to-deep-copy-a-list) the `sequence`. – shriakhilc May 16 '19 at 04:52
  • 1
    Thank you very much TheGamer007, this should fix my code and I learned something new. – WingsOverEden May 16 '19 at 14:57

0 Answers0