I am trying to solve leetcode problem 6 and encountered something that I can't understand. The problem is asking me to use a zigzag pattern to rearrange a string column by column and output it row by row. In my function I used two different ways to create my initial list that stores the output. However, one of them works and the other doesn't. Can someone help me understand why this is happening? Here are the methods I used to initilize the list down below. Since there are multiple rows to output and I want to keep track of which row contains what letter, I thought I could create a list includes muliple sublists.The first method worked just fine, however, the second one could not distinguish which sublist contains which letter and appened the entire string after iterations.
zigzag = [[] for x in range(n)]
zigzag = [[]]*n
for crct in l:
zigzag[row].append(crct)
Here's the output for the first method: [['P', 'I', 'N'], ['A', 'L', 'S', 'I', 'G'], ['Y', 'A', 'H', 'R'], ['P', 'I']]
And the output for the second: [['P', 'A', 'Y', 'P', 'A', 'L', 'I', 'S', 'H', 'I', 'R', 'I', 'N', 'G'], ['P', 'A', 'Y', 'P', 'A', 'L', 'I', 'S', 'H', 'I', 'R', 'I', 'N', 'G'], ['P', 'A', 'Y', 'P', 'A', 'L', 'I', 'S', 'H', 'I', 'R', 'I', 'N', 'G'], ['P', 'A', 'Y', 'P', 'A', 'L', 'I', 'S', 'H', 'I', 'R', 'I', 'N', 'G']]