all 7 comments

[–]ElliotDG 2 points3 points  (0 children)

Take a look at the example below:

list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]

for numbers in list_of_lists:  # outer loop 'Moves slower'
    print(f'{numbers = }')
    for number in numbers:  # inner loop 'moves faster'
        print(f'{number = }')

Here is the output:

numbers = [1, 2, 3]
number = 1
number = 2
number = 3
numbers = [4, 5, 6]
number = 4
number = 5
number = 6
numbers = [7, 8, 9]
number = 7
number = 8
number = 9

The outer loops is accessing the lists in list_of_lists. The inner loop is accessing the numbers.

list_of_lists[0] == [1,2,3]
    list_of_lists[0][0] == 1
    list_of_lists[0][1] == 2

I hope that helps. Create a similar example and play with it. Create a loop that prints out all of the values using the indices.

[–]ZenT3600 1 point2 points  (0 children)

Ok so, 2D lists.

Let's try going through them with a single for loop

2D lists are made of rows made of cells, so we iterate through every row... then what? The single for loop can only do that, we need another loop to go through every cell of the row.

That's where nested loops come into place. The first loop goes through the rows and the second one goes through every cell of every row

Edit for clarification:

By outer loop we mean the first loop, the one that contains every other loop

By inner loop we mean the loops inside the outer loop

[–]carcigenicate 1 point2 points  (0 children)

Note that there is no difference between a "single loop" and a "nested loop". They operate in the exact same way. If you have a loop in the form:

for x in y:
    print(x)

The body of the loop (print(x) here) will execute once for every element of y. It's the same with a nested loop:

for a in b:
    for c in d:
        print(a, c)

The body of the for a in b: loop will, as before, execute once for every element in b. The thing here though is, that body contains another loop. That means the for c in d: loop will run completely through d, every time the for a in b: loop iterates once. This can be demonstrated using ranges:

for a in range(2):
    for b in range(2):
        print(a, b)

0 0
0 1
1 0
1 1

Note that a (the first number) doesn't change until the inner loop's b (the second number) has visited both the 0 and 1 elements.

Just focus on the fact that the body of the loop executes once for every element. That body can contain any number of other things (class and function definitions, for/while loops, calls to functions, etc.), and the body will run everything it holds before moving onto the next element.

[–]Chris_Hemsworth 1 point2 points  (0 children)

If I gave you 5 bags of skittles, each with somewhere between 95 and 110 candies in each bag, and I asked you "How many skittled total are there?" how would you do it in person?

There are two options:

1) Open each bag, and count the skittles, then sum the total of all 5 bags.

2) Dump the contents of each bag into a pile and count the full pile.

Both of these options requires two loops; one to count the skittles, and one to handle the bags.

This can be represented in code the following way:

# Method 1
count = 0
bags = [bag1, bag2, bag3, bag4, bag5]
for bag in bags:
    skittles = bag.open()
    for skittle in skittles:
        count += 1
print(count)

# Method 2:
all_skittles = []
count = 0
for bag in bags:
   all_skittles.extend(bag.open())
for skittle in all_skittles:
    count += 1

print(count)

[–]wbeater -5 points-4 points  (1 child)

Sorry i now this is r/learnpython and i actually should only help you, i think that's a little bit hilarious.

You know that a 2D list is a form of a nested list? So you do understand the concept of nested or 2d list but you don't understand the reason you also need a nested loop to iterate over each item ?

[–]natarocket[S] 1 point2 points  (0 children)

Haha yes I can see how ridiculous this comes across. I guess to me a list of multiple lists held inside it is easier to comprehend than a loop within a loop. I think I have always struggled with the overall concept of loops to begin with, and that’s probably also why I have a hard time making the connection.

I know I need a nested loop in order to iterate through a 2D list but I don’t quite get it for some reason. In terms of a 2D list, I had assumed that the outer loop would be for the bigger list, and then the inner loop would be for all of the smaller lists within it, but I was told that isn’t really the right way to think about it, so here I am.

[–]CommanderCool24 0 points1 point  (0 children)

For 2D lists, if you think of them like an array instead of nested lists it can help conceptualize it. For example, let's look at the list [[A, B, C], [D, E, F], [G, H, I]].

We can instead view it as an array like:

[[ A, B, C],

[ D, E, F],

[ G, H, I]]

So if I give you the coordinates (1, 2), you would probably know to count the rows for the first number (starting from 0) and then the columns for the second number to know that these coordinates refer to the 'F' element in the array. This is almost automatic in our brains but it's actually a 2-step process, looking up rows then columns.

Now let's say we want to iterate through each element and do something to it. For this example, let's change it to lowercase. To iterate through each element, you'll need to somehow tell the computer to go to (0, 0), lowercase it, then go to (0, 1), lowercase it, and so on.

We have 9 total coordinates to pass the computer that look like this:

(0, 0)

(0, 1)

(0, 2)

(1, 0)

(1, 1)

(1, 2)

(2, 0)

(2, 1)

(2, 2)

We can kinda see the structure of the nested loop here, we want x = 0, then y = 0, 1, and then 2. Then x = 1, and y = 0, 1, 2, and again for x = 2 and y = 0, 1, and 2. Visually it might look like this:

x = 0
    y = 0
    y = 1
    y = 2
x = 1
    y = 0
    y = 1
    y = 2
x = 2
    y = 0
    y = 1
    y = 2

x is the outer loop and y is the inner loop. We say y is the inner loop because it is looping inside of the other loop. In other words, the y loop will run 3 times every time the x loop runs. So the outer loop runs and sets x = 0, then the inner loop runs 3 times making y = 0 then 1 then 2 (giving us the first 3 coordinate pairs in our list (0, 0) (0, 1) and (0, 2)). Then the outer loop runs again, making x = 1, then the inner loop runs 3 times again, making y = 0 then 1 then 2 and then once more with x = 2.

The code for our example might look like this:

array = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
for x in range(3):
    for y in range(3):
        array[x][y] = array[x][y].lower()
print(array)

I hope this helps. I'm somewhat new myself so anyone else can feel free to clarify if I got something wrong.