0

I want to iterate over a 2d python list. However, I have noticed a difference when it iterates over an empty 2d array vs a nonempty 2d array. For example

    mat = [[]] * n
    print("initial", mat)
    
    count = 0
    for row in mat:
        print("row:", row)
        for _ in range(2):
            print(_)
            row.append(count)
            count += 1
            print(mat)
        print("\n")
    return mat
mat = empty(2)

This is what it prints. It is appending the count to both the sub-arrays instead of just one

initial [[], []]
row: []
0
[[0], [0]]
1
[[0, 1], [0, 1]]


row: [0, 1]
0
[[0, 1, 2], [0, 1, 2]]
1
[[0, 1, 2, 3], [0, 1, 2, 3]]

However when I change the code to this

def nonempty(n):
    mat = [['a'],['b']]
    print("initial", mat)
    
    count = 0
    for row in mat:
        print("row:", row)
        for _ in range(2):
            print(_)
            row.append(count)
            count += 1
            print(mat)
        print("\n")
    return mat
mat = empty(2)

This is what it prints

initial [['a'], ['b']]
row: ['a']
0
[['a', 0], ['b']]
1
[['a', 0, 1], ['b']]


row: ['b']
0
[['a', 0, 1], ['b', 2]]
1
[['a', 0, 1], ['b', 2, 3]]

In this case, it only changes one sub array at a time instead of two. Can anyone tell me why this is happening. Is it because of how python lists work?

1 Answer 1

1

The problem is in mat = [[]] * n where all empty arrays are in fact the same instance

Same object

http://pythontutor.com/visualize.html#code=mat%20%3D%20%5B%5B%5D%5D%20*%205%0Amat%5B1%5D.append%281%29%0Aprint%28mat%29&cumulative=false&curInstr=3&heapPrimitives=true&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

So you can create your mat as mat = [ list() for _ in range(n) ] to be sure that no empty list is shared.

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

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.