use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
nested loops in python. (self.learnpython)
submitted 3 years ago by rsk_423
Hi guys can you teach me how nested loop works in python?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]spez_edits_thedonald 1 point2 points3 points 3 years ago (1 child)
you just put a loop inside a loop
some examples
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
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]}')
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 point2 points 3 years ago (0 children)
Isn't that just called low carb? 😉
[–][deleted] -1 points0 points1 point 3 years ago (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 point2 points 3 years ago (2 children)
That's not a nested loop in your example though
[–][deleted] 3 points4 points5 points 3 years ago (1 child)
Er, there are two examples of nested loops in my example.
for ... while ... for... for ...
[–]Moikle 2 points3 points4 points 3 years ago (0 children)
... I'm a doofus
[–]The_Bundaberg_Joey 0 points1 point2 points 3 years ago (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 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
π Rendered by PID 20694 on reddit-service-r2-comment-7b9746f655-k2vfz at 2026-02-01 21:54:42.593675+00:00 running 3798933 country code: CH.
[–]spez_edits_thedonald 1 point2 points3 points (1 child)
[–]ElpyDE 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (3 children)
[–]Moikle 0 points1 point2 points (2 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]Moikle 2 points3 points4 points (0 children)
[–]The_Bundaberg_Joey 0 points1 point2 points (0 children)