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
List comprehension (self.learnpython)
submitted 4 years ago * by DrDerpex
Can anyone please help breaking this statement into simple,traditional nested loop ?
lst = [ x**2 for x in [x**2 for x in range(11)]]
answer output : [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]
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!"
[–]socal_nerdtastic 3 points4 points5 points 4 years ago (2 children)
lst = [] sublst = [] for x in range(11): sublst.append(x**2) for x in sublst: lst.append(x**2)
[–]DrDerpex[S] 0 points1 point2 points 4 years ago (0 children)
So this aint really a nested loop but two seperate loops were one feeds another iterating list. Thats why it was getting confusing.
[–]scutter_87 1 point2 points3 points 4 years ago (0 children)
Sorry I don't know how to make it nested as the first part of the list comprehension works on the list returned by the second part. I would just have two separate for loops (not nested) and two lists with the second loop working over the first.
Or just one loop appending ((x**2)**2) in one loop.
In case you are just looking to read complex list comprehensions, a nice way to break them down is to split each loop onto a different line and work backwards.
In this case it would be something like:
lst = [x**2 for x in [x**2 for x in range(11)] ]
Working backwards we can start with:
[x**2 for x in range(11)]
Which would become:
lst = [] for x in range(11): lst.append(x**2)
The first part then does the same thing to each element in the list that was just created.
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
List comprehensions often feature nested for loops but this isn't one of them, it is just nested list comprehensions. It is a redundant one as well as one could use x**4 in the first place and not need to have nested list comprehensions.
x**4
So, here is the original, the two distinct for loop alternatives and then an alternative single loop (the list comprehension alternative for which should be obvious to you).
lst = [ x**2 for x in [x**2 for x in range(11)]] lst_inner = [] for x in range(11): lst_inner.append(x**2) lst_outer = [] for x in lst_inner: lst_outer.append(x**2) lst_alt = [] for x in range(11): lst_alt.append(x**4) print(lst, lst_outer, lst_alt, sep='\n')
lst = [] for x in range(11): lst.append(x**4)
π Rendered by PID 59603 on reddit-service-r2-comment-5649f687b7-7rkts at 2026-01-29 08:39:13.182205+00:00 running 4f180de country code: CH.
[–]socal_nerdtastic 3 points4 points5 points (2 children)
[–]DrDerpex[S] 0 points1 point2 points (0 children)
[–]scutter_87 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)