Alex Hall
Alex Hall

Reputation: 304

Python struggling to understand for loops with a list

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', '.', '.', '.'],
       ['.', '.', '.', '.', '.', '.']]

 def gridOutput(grid):
    for s in range(len(grid[0])):
        print()
        for i in range(len(grid)):
            print(grid[i][s],end='')
 gridOutput(grid)

This is printing out what I expect to be printed out. It is looping through the each element of each list by one. So like [0,1] [0,2] [0,3] [0,4] [0,5] etc

But I don't get how the code is doing this.

range(len(grid[0])): 

Why use len when this returns the length of the list? and I don't get what [0] is doing, if i set it to any other integer up to 9 this works exactly the same.

Could someone please explain what this is doing, I have been trying to figure this out for 2 days and I now need some guidance.

Upvotes: 0

Views: 1009

Answers (3)

Karl
Karl

Reputation: 1724

When you do len(grid) that is only going to return the length of how many lists or 'rows' are in your grid. However, this does not tell you anything about the length of each list/row. To get that information, you access a list and get the length of that collection assuming they are all the same length.

len(grid[0]) is going to give you the length of the first list (at index 0), which would be the amount of columns basically.

And why this is passed into the range(x) call is this is going to loop from 0 to x of whatever you pass in. So that loop is going to loop through all the indices of the columns in the outer loop.


Edit: To change your code so it supports sub-lists of different lengths in your grid, you should get the length of each sub-list inside the list instead of only the length of the first sub-list. To do this you have to swap the order of your for loops like so:

def gridOutput(grid):
    for i in range(len(grid)):
        print()
        for s in range(len(grid[i])): # Get length of current sub-list
            print(grid[i][s], end='')

Upvotes: 2

Martin Z
Martin Z

Reputation: 27

len(grid[0]) gets the number of elements in the first row of your grid. In this case 6 => the number of columns. So the first loop runs 6 times, and each iteration creates a second loop: for i in range(len(grid))

len(grid) gets the number of elements in the grid itself, in your case 9 => the number of rows.

Quick walkthough:

In the first iteration s=0, i=0 => first column in first line
In the second iteration s=0, i=1 => fist column in second line and so on...

Hope this helps.

Upvotes: 1

A.Lee
A.Lee

Reputation: 265

it basically prints out each element from 2 dimensional list
grid[0] => ['.', '.', '.', '.', '.', '.']
len(grid[0]) => 6
for s in range(len(grid[0])) => s=0, 1... to 5
you can use numpy to clean to code

Upvotes: 2

Related Questions