0

I practiced on Character Picture Grid exercise from Automate the Boring Stuff with Python. and suppose to rotate the list grid by 90 degrees. I understand its logic and process.

grid = [['.', '.', '.', '.', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['.', 'O', 'O', 'O', 'O', 'O'],
        ['O', 'O', 'O', 'O', 'O', '.'],
        ['O', 'O', 'O', 'O', '.', '.'],
        ['.', 'O', 'O', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

the desirable output looks like this.

['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.']
['.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.']
['.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.']
['.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.']
['.', '.', '.', 'O', 'O', 'O', '.', '.', '.']
['.', '.', '.', '.', 'O', '.', '.', '.', '.']

So I intended to prepare a new list with 6 items (6 blank lists) then append value from the original list (grid) to rotate it. But from my following code, the function appends into every item of the new list at once instead of appending the list by order of for loop.

def transpose(heart):
    outcome = []
    row = []
    for i in range(len(heart[0])):
        outcome = outcome + [row]
    for i in range(len(outcome)):
        for y in range(len(heart)):
            outcome[i].append(heart[y][i])
    for i in outcome:
        print(i)

output

['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']
['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']
['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']
['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']
['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']
['.', '.', 'O', 'O', '.', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', 'O', 'O', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', 'O', 'O', 'O', 'O', 'O', '.', '.', '.', '.', '.', 'O', 'O', 'O', '.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.']

So, I tried a different way from comment to create a new list before starting to rotate it while keeping other parts just the same. then this one gives the correct output.

def transpose(heart):
    outcome = [[] for i in range(len(heart[0]))]
    for i in range(len(outcome)):
        for y in range(len(heart)):
            outcome[i].append(heart[y][i])
    for i in outcome:
        print(i)

Why are they different? since these 2 ways give just the same new list.

#first way
outcome = [[] for i in range(len(grid[0]))]
#second way
outcome1 = []
row = []
for i in range(len(grid[0])):
    outcome1 = outcome1 + [row]
outcome == outcome1
True

Thank you so much for your time. This is my first question.

T. Lee
  • 3
  • 1
  • Then please mark it as a duplicate – Tomerikoo Jan 25 '21 at 09:33
  • You're welcome. Please mark this question as a duplicate, so future viewers can quickly find the answer I linked. – couka Jan 25 '21 at 09:37
  • Or you could just use [zip](https://docs.python.org/3/library/functions.html#zip) to rotate: `rotated = list(zip(*grid))`. –  Jan 25 '21 at 15:05

1 Answers1

0

The key to the problem is in these two lines:

def transpose(heart):
    outcome = []
    row = [] # <- Key1
    for i in range(len(heart[0])):
        outcome = outcome + [row] # <- Key2
    for i in range(len(outcome)):
        for y in range(len(heart)):
            outcome[i].append(heart[y][i])
    for i in outcome:
        print(i)

What you are creating in the "Key 2" line is a list of references to the list named row. So any time you call append on any item of the list "outcome" the "row" list appends a new item. Which is the reason of the bad output. In the second example you are initializing each item of "outcome" as a different empty list

To see the error more clearly you can run the following code:

outcome = []
row = ["Hello! I'm the row list",] 
for i in range(5):
     outcome = outcome + [row]
print(outcome)

A simple way to fix it would be to substitute row by an empty list

outcome = []
for i in range(5):
     outcome = outcome + [[]]
outcome[0].append('I am The first item')
outcome[2].append('I am The Third item')
print(outcome)

Edit: fixed typos

rperezsoto
  • 66
  • 3