Python struggling to understand for loops with a list

python for loop
for loop string python
python for loop step
python for each
for loop in python 3
types of loops in python
for loops explained python
if statement for list in python
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])):
        for i in range(len(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.


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.

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)):
        for s in range(len(grid[i])): # Get length of current sub-list
            print(grid[i][s], end='')

Visual Basic/Loops, while loop Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. In Python this item can be a list of words, numbers, etc, or if given a string, it will take characters as the items. Another way to envision it is that you have a shopping basket full of items, and the cashier is looping through them one at a time: for eachitem in mybasket: # add item to total # go to next item.

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

Python - Loops, for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. You have to use Python for loop and looping over a list variable and print it in the output. The list variable is the variable whose values are comma separated. All the items are enclosed within the square brackets. To print all the variables, you have to pass the variable as an argument of the print statement.

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.

Programming – The Purpose Of Loops – DPS Computing, What do you gain from using loops in your code? For Loops and List Comprehension in Python What are For Loops? for loops are traditionally used when you have a block of code which you want to repeat a fixed number of times. The Python for statement iterates over the members of a sequence in order, executing the block each time. Source. Can you guess the output?

Can't Understand For Loops Python, I'm trying to create a for loop that will go through a list of numbers and add them all together but I'm really struggling with how the syntax works. Python For Loops A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

Help please! I don't understand loops - Python, I am really struggling with loops, have gone through the course and challenges but **My solution** list1=range(0,237) # iterating each number in list for num in r/learnprogramming - I don't understand "for" loops (Python). Python : How to create a list and initialize with same values; C++11 - std::all_of() - Algorithm tutorial & example; Different ways to Iterate / Loop over a Dictionary in Python; Check if all elements in a list are None in Python; Python: 4 ways to print items of a dictionary line by line; Python : Get number of elements in a list, lists of

i'm struggling to understand loops : learnpython, I want to make a small script that pulls a list of tickers from a specific stock index every week. I am following Al Sweigarts "Automate the Boring Stuff With Python"  Conclusion. Python for loops are important and they are used widely in data scripts. The syntax is simple, but as you have seen, to fully understand the logic behind it requires a little bit of brainwork. But by reading this article, you got through it and now you have a solid foundation to build on.

  • Get the first grid item, get its length, then get a range of numbers from 0 to that length - 1. Changing the index doesn't change anything for you because all the grid items have the same length.
  • (There are better ways to do this printing without explicitly referring to the length of a list anywhere, but this code isn't using the better ways.)
  • grid[0] contains all your list grid[0][0] would contain the first array of your grid array So grid[0] len should be 8 grid [0][0] len should be 5
  • So len(grid) is getting the amount of lists within the list? and len(grid[0] is getting the amount of value within the lists that are within the list? I think it's making sense
  • @AlexHall Yeah that's correct. Except it is only getting the amount of values in the first list because it gets it at index 0.
  • How come when I change index 0 to another integer, the output is exactly the same?
  • @AlexHall that is because your sub-lists all have the same length in your grid. If you had one of the sub-lists have more/less elements it would return that length.
  • Added one more value to the list at index[0], this means the length is 7. So when the second loop is called it would error because the other sub-lists only contain 6 values. When it gets the first value in the first for loop does it go to the second loop straight away?