I want to generate a square-like text structure in Python using nested lists. An array in which I could modify specific elements at specific indexes with a function like:
def locate(self, x, y):
return self.array[x][y]
The problem is that as I try to modify an element using multilevel list indexing, many elements are modified as well. Here's my code:
length = 5
tip_row = list('+' * length)
mid_row = list('+' + ' '*(length-2) + '+')
array = [mid_row for _ in range(length-2)]
array.insert(0, tip_row)
array.append(tip_row)
array[3][3] = 'o'
for row in array:
print(' '.join(row))
I expected the output to be:
+ + + + +
+ +
+ o +
+ +
+ + + + +
But the actual output is:
+ + + + +
+ o +
+ o +
+ o +
+ + + + +
Apparently, I cannot refer to a specific list in the array. Why is this happening? How can I achieve the expected output?