-1

I have a 2d array where I'm trying to iterate through the thing and print ouf every element. For loops in Python are annoying/confusing because you don't have the expressability as you do in C++ with being able to set conditions/increments.

Anyways, when I try to print out the contents of this array, it prints out the contents of the array 3 times instead of just once.

twoDarray = [[10, 15, 20], [25, 30, 35], [45, 50, 60]]
for i in twoDarray:
    for num in twoDarray:
        print(num)

Why is my nested for loop not working as expected?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Shah Jacob
  • 137
  • 1
  • 9
  • 5
    You are looping through the whole array again in the inner loop instead of looping through the row. – Advay168 Nov 14 '21 at 12:43
  • 4
    Try `for num in twoDarray:` --> `for num in i:` – balderman Nov 14 '21 at 12:43
  • 1
    but loop variables in python don't mean anything right? i could acll the two loop variables whatever i want. in c++ you have to define them like `int i =0` and `int j=0`. why does it matter if i call the second for loop variable `num`. also big question: why am i am taking the `num` variable and using that to iterate through `i`? im confused – Shah Jacob Nov 14 '21 at 12:51
  • 1
    In your own words: when you do `for i in twoDarray:`, what do you think will be the value of `i` the first time through the loop? The second time? The third? Why? When you try using a debugger or `print` statements to test that, does it work as you expect? Where you have `for num in twoDarray:`, same question. Now: what *should* the value of `num` be the first time through the inner loop? Where do you expect that value to come from? Therefore, according to your understanding of how the `for` loop works, how should you write the loop? In particular, what should go after `in`? – Karl Knechtel Nov 14 '21 at 12:53
  • In your code ``twoDarray`` is not the loop variable, it is the iterable. – MisterMiyagi Nov 14 '21 at 12:53
  • "why does it matter if i call the second for loop variable num" It doesn't matter, and nobody suggested that it matters. You were told to change your existing code, `for num in twoDarray:`, to `for num in i:`. What part is actually changing there? Hint: not `num`. – Karl Knechtel Nov 14 '21 at 12:54
  • (The names don't mean anything in C++, either, and you could perfectly well write `for (int num=0; ...)`. – Karl Knechtel Nov 14 '21 at 12:55
  • 1
    Might be relevant for understanding: [How does a Python for loop with iterable work?](https://stackoverflow.com/questions/1292189/how-does-a-python-for-loop-with-iterable-work) – MisterMiyagi Nov 14 '21 at 12:56
  • Please also read https://stackoverflow.com/help/dont-ask. Your opinion about Python as it compares to C++ is off topic and does not help answer the question. – Karl Knechtel Nov 14 '21 at 12:56
  • @KarlKnechtel so im trying to think. for the outer for loop, you are accessing the inner 3 small arrays. okay, in this case what is the second for loop doing? i guess my question is when you said 'where you have `for num in twoDarray`:, what should the value of be the first time through the inner loop'. i dont know the answer to this question. what is the value the first time through the inner loop when i wrote `for num in twoDarray`? – Shah Jacob Nov 14 '21 at 12:56
  • 1
    "for the outer for loop, you are accessing the inner 3 small arrays. okay, in this case what is the second for loop doing? " Wrong question. What do you **want it** to do? You want it to access the individual integers that are in one of the small lists (we don't call them arrays!), right? Okay, so, **because** you used the outer `for` loop to "access the inner 3 small [lists]", and **because** you wrote `for i`, that means that `i` currently **is** one of the small lists. – Karl Knechtel Nov 14 '21 at 12:59
  • 2
    So in the inner loop, you have a "small list" named `i`, and you want to access the individual elements of `i`. You write that `for` loop the same way you would write any other `for` loop: by **deciding** a name to use for the individual elements (you previously chose `num`; let's stick with that) on the left, and **specifying** where the elements come from on the right. Thus: `for num in i:`. – Karl Knechtel Nov 14 '21 at 13:01
  • This process is, I would argue, [much easier](https://stackoverflow.com/questions/4383250/why-should-i-use-foreach-instead-of-for-int-i-0-ilength-i-in-loops/4383321#4383321) to understand than C++'s. C++ has warped your thinking to the contrary. – Karl Knechtel Nov 14 '21 at 13:03
  • oh, so on the outer loop, i 'takes the place' of a sub list right? so you have to do `for j in i` to be able to print all of the elements of the inner array right? – Shah Jacob Nov 14 '21 at 13:11

3 Answers3

3

You are iterating through the same list twice. Note that your in variables are twoDarray in both loops.

It should be something like:

twoDarray = [[10, 15, 20], [25, 30, 35], [45, 50, 60]]
for line in twoDarray:
    for num in line:
        print(num)
Tmpod
  • 176
  • 1
  • 10
  • why is the second in variable is line? why am i using the in variable from the outer loop for in variable in the inner loop? – Shah Jacob Nov 14 '21 at 12:53
  • 1
    "why is the second in variable is line?" Because this version says `for line in twoDarray:`. "why am i using the in variable from the outer loop for in variable in the inner loop?" The reason that you match up `line` like this is because the outer loop makes `line` a name for each of the rows in turn (by getting those values from the overall structure), and in the inner loop, you want to get values from the rows. – Karl Knechtel Nov 14 '21 at 13:05
  • 1
    Python's syntax here means **exactly what it means in English**, just with some words omitted as shorthand. `for line in twoDarray:` exactly means "do the following, `for` each thing, which will be called `line` in the task description below, that is found `in` the `twoDarray`". – Karl Knechtel Nov 14 '21 at 13:07
0

Both loops iterating the same object...

for i in twoDarray:
    for num in i:
Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
0

Here i is also iterating twoDarray and num is also iterating twoDarray. That means i values will be the inner lists and num values will also be inner lists. You've to iterate the inner list in the 2nd loop. Change for num in twoDarray to for num in i. Now your num values will be the elements of the inner list (your desired output). Your code:

twoDarray = [[10, 15, 20], [25, 30, 35], [45, 50, 60]]
for i in twoDarray:
    for num in i:
        print(num)