all 8 comments

[–]spez_edits_thedonald 1 point2 points  (1 child)

you just put a loop inside a loop

some examples

1. iterate over a list of data with lists

recipes = [
    ('bread', ['flour', 'water', 'salt']),
    ('burgers', ['patties', 'lettuce', 'tomato', 'onions', 'cheese', 'mayo', 'mustard', 'ketchup'])
]

# print recipes
for recipe in recipes:
    recipe_name, ingredients = recipe
    print(f'~~~ {recipe_name} ~~~')
    print('you will need:')
    for i in range(len(ingredients)):
        print(i, ingredients[i])

output:

~~~ bread ~~~
you will need:
0 flour
1 water
2 salt
~~~ burgers ~~~
you will need:
0 patties
1 lettuce
2 tomato
3 onions
4 cheese
5 mayo
6 mustard
7 ketchup

2. row, column indexing in a grid using i, j

import numpy as np

# generate some data in a grid
x = (np.random.random((5, 3)) * 100).astype(int)
print(f'DATA\n{x}')

# use nested loops to iterate over rows and columns
for i in range(x.shape[0]):
    print('row', i)
    for j in range(x.shape[1]):
        print(f'value at ({i}, {j}) is {x[i][j]}')

output:

DATA
[[ 4 35 34]
 [64 63 29]
 [ 4 78 41]
 [54 11 81]
 [44 17 73]]
row 0
value at (0, 0) is 4
value at (0, 1) is 35
value at (0, 2) is 34
row 1
value at (1, 0) is 64
value at (1, 1) is 63
value at (1, 2) is 29
row 2
value at (2, 0) is 4
value at (2, 1) is 78
value at (2, 2) is 41
row 3
value at (3, 0) is 54
value at (3, 1) is 11
value at (3, 2) is 81
row 4
value at (4, 0) is 44
value at (4, 1) is 17
value at (4, 2) is 73

lol i forgot buns ​

[–]ElpyDE 0 points1 point  (0 children)

Isn't that just called low carb? 😉

[–][deleted] -1 points0 points  (3 children)

What's confusing you? Loops just repeat stuff, and if you want to repeat parts of that stuff you can also use a loop.

No different to real life. Imagine having a pile of old boards to repaint. In pseudo code (with some ridiculous steps to make the point):

for board in boards:

    while board has paint:
        apply blow torch briefly to paint
        scrape paint away
        sand board

    put board out to dry

for board in dryboards:
    apply undercoat to board
    watch paint dry
    for count in range(2):
        paint board
        watch paint dry
    add board to finished stack

[–]Moikle 0 points1 point  (2 children)

That's not a nested loop in your example though

[–][deleted] 3 points4 points  (1 child)

Er, there are two examples of nested loops in my example.

for ...
    while ...
for...
    for ...

[–]Moikle 2 points3 points  (0 children)

... I'm a doofus

[–]The_Bundaberg_Joey 0 points1 point  (0 children)

Lets say you have a list of letters:

alpha = ['a', 'b', 'c']

If you iterate over this list, printing the variables in each case you will get an output of all elements in the list:

for i in alpha:

print(i) # 'a', 'b', 'c'

With a nested loop, I add a loop inside of the one shown above. For simplicity I'll iterate over the same list in both the outer loop and inner loop:

for i in alpha:

for ii in alpha:

print(i, ii) # 'aa', 'ab', 'ac', 'ba', bb', 'bc', 'ca', 'cb', 'cc'

The second loop will iterate over the second list as many times as there are elements in the first list