0
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(5):
    b = a
    for i in range(2):
        b.append(b[0])
        b.pop(0)
    print("a: ", a)
    print("b: ", b)

I have had an issue, in Python 3.6.4, where I would set a list to be the contents of another list. Then, I would append the first item of that list (item 0) to the end, then remove it from the beginning. After I have done this, the original list is overwritten! Try to figure out what the code above would output, then run it (or see the output below) I expect:

a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]

The actual result:

a:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
b:  [2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
a:  [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
b:  [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
a:  [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
b:  [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
a:  [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
b:  [8, 9, 0, 1, 2, 3, 4, 5, 6, 7]
a:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b:  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0 Answers0