I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.
I find the book quite easy to understand and the exercises are fun. There is one in particular that I just solved but honestly, I am not satisfied with my answer.
In the question, one is given a grid as follows:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
Using loops only, one is then required to produce the following output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
My solution (which works) to this problem is as follows:
for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')
print()
My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.
My question, therefore, is how can I retrieve the len
of grid[0] in general variable terms?
In other words, how can I retrieve the length of a list within a list?