Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested loop indexing
#1
Basically, I'm just trying to understand the logic for the nested loop inputs and outputs. The first nested loops seems to append either a # or a space for 20 values 60 times. So, I understand that I'm inputting either a # or space 20 times per row? What confuses me is the second nested for loop. The WIDTH and HEIGHT are inverted. You start off X with width and then y with height. I don't understand this. In my second nested loop I tried to make the range of the outer loop WIDTH and the inner HEIGHT, but I get an indexing error but I don't understand why? I'm just using the same indexing I used in the previous? How come I have to invert the WIDTH AND HEIGHT for the second nested loop?

WIDTH = 60
HEIGHT = 20

nextCells = []
for x in range(WIDTH):
    column = []
    for y in range(HEIGHT):
        if random.randint(0,1):
            column.append('#')
        else:
            column.append(' ')
    nextCells.append(column)

while True:
    print('\n\n\n\n\n')
    currentCells = copy.deepcopy(nextCells)

    for y in range(HEIGHT):
        for x in range(WIDTH):
            print(currentCells[x][y],end='')
        print()
Reply
#2
To match the first for loops the second loops should be
for x in range(WIDTH):
    for y in range(HEIGHT):
not
for y in range(HEIGHT):
    for x in range(WIDTH):
Also, note you would probably want the inner loop to be the WIDTH and the outer loop to be the HEIGHT
Reply
#3
I see, although not quite. After reviewing how these lists interacted with these nested loops I'm more confused. I'm coming from C++ and I know that arrays are used for stuff like this but it seems different. I don't see why accessing a list would require an x and y since it doesn't seem to be a multidimensional list. Then again I have no idea.
Reply
#4
(Aug-04-2020, 06:59 AM)Morte Wrote: since it doesn't seem to be a multidimensional list.

Assuming "it" is currentCells, why do you think that? It's a copy of nextCells and nextCells appears to be a multidimensional list (really just a list of lists).
Reply
#5
Now I see what I was doing wrong. I looked over how deepcopy was working and treated it like an array instead of a list so I need to review more. Thank you for bringing the list of list up. Now wonder I was indexing incorrectly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Big O runtime nested for loop and append yarinsh 4 1,373 Dec-31-2022, 11:50 PM
Last Post: stevendaprano
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,570 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  How do I add another loop to my nested loop greenpine 11 4,537 Jan-12-2021, 04:41 PM
Last Post: greenpine
  Matrix indexing and initialization in " for in" loop QuintenR 2 1,845 Dec-23-2020, 05:59 PM
Last Post: QuintenR
  Error on nested loop : Invalid syntax dvazquezgu 3 3,226 Nov-25-2020, 10:04 AM
Last Post: palladium
  Nested for loop not looping puttingwordstogether 0 1,702 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  Help: for loop with dictionary and nested lists mart79 1 1,861 Apr-12-2020, 02:52 PM
Last Post: TomToad
  "for loop" not indexing correctly? melblanc 4 2,545 Jan-24-2020, 03:11 PM
Last Post: melblanc
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,314 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  Nested Loop for user input Ads 2 3,546 Dec-30-2019, 11:44 AM
Last Post: Ads

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020