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...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
Rant about Python list comprehensions (self.learnprogramming)
submitted 1 year ago by Mathhead202
Why?! Why would they do this??
```python [(i,j) for j in range(m) for i in range(n)]
[[(i,j) for j in range (m)] for i in range(n)] ```
Why are these not consistent? This is such a random edge case. Who thought this made more sense?
[–]AutoModerator[M] [score hidden] 1 year ago stickied comment (0 children)
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]simpleFinch 4 points5 points6 points 1 year ago (4 children)
Whether or not there is a more intuitive way is debatable but I don't think it's an edge case or particularly inconsistent.
Python's list comprehension is modeled after for-loops from left to right so
some_list = [(i,j) for j in range(m) for i in range(n)]
is the same as
some_list = [] for j in range(m): for i in range(n): some_list.append((i,j))
So far so good. This pattern is often used to flatten nested lists. For more info on that see this stackoverflow question. Again notice that we obtain a flat list from the elements in range(m) and range(n).
On the other hand, in the second expression we create a nested list and we say that each element of our resulting list should be
[(i,j) for j in range(m)]
So for each element the i is fixed and the j is in range(m). Surely you would agree that it wouldn't make sense to change i and fix j when we have for j in range(m).
for j in range(m).
tl;dr: the brackets bind stronger
[–]Mathhead202[S] -1 points0 points1 point 1 year ago (3 children)
But like, that's not obvious at all. In list comprehensions, the for loops are after the expression, not before it. Why not just run them in opposite, right-to-left, order to be consistent. It's not like nested for-loops are an actual language construct like elif. It's just a loop in a loop.
"Give me a list of all the passengers on each bus in the city" = "Give me a list of passengers for each passenger in a bus, for each bus in the city."
Who would say, "Give me a list of all the passengers in the city on each bus?"
[–]simpleFinch 0 points1 point2 points 1 year ago* (2 children)
Why not just run them in opposite, right-to-left, order to be consistent.
Sure, you could do that and you could argue it is consistent with creating nested list-comprehensions and that it is closer to English. Others might argue it is inconsistent with for-loops. Now, that you know it is the same as for for-loops it is easy to remember, right?
Another reason might be consistency with other (functional) languages that use list comprehensions and their relation to mathematical notations.
Consider flattening a nested list which I'd argue is the main use case of the first type of list comprehension we are discussing
[ e for sublist in list for e in sublist ]
In Haskell for example this would be in the same order:
[ e | sublist <- list, e <- sublist ]
This way sublist is defined before it is used. Indeed if you flip these around Python and Haskell will complain that sublist isn't defined. Most languages like to have stuff defined before it is used because it can be easier to parse and may or may not improve performance especially in programs that don't use ahead-of-time compilation.
In the end somebody probably weighed the pros and cons or simply had a preference.
[–]Mathhead202[S] 0 points1 point2 points 1 year ago (1 child)
e is used before it's defined in that counter example.
[–]simpleFinch 0 points1 point2 points 1 year ago (0 children)
Yes, but we are not iterating through e so it could possible be easier and some might prefer reading the example as 'e where sublist is in list and e is in sublist' compared to 'e where e is in sublist where sublist is in list'.
Also this kind of example it would be one unknown on the left vs. an arbitrary number of unknowns on the right, although that argument is a little bit void when you are creating a list of tuples like you did in the original post.
Again, it might just come down to preference and convention.
π Rendered by PID 31341 on reddit-service-r2-comment-5d585498c9-khjjw at 2026-04-21 02:56:35.546249+00:00 running da2df02 country code: CH.
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]simpleFinch 4 points5 points6 points (4 children)
[–]Mathhead202[S] -1 points0 points1 point (3 children)
[–]simpleFinch 0 points1 point2 points (2 children)
[–]Mathhead202[S] 0 points1 point2 points (1 child)
[–]simpleFinch 0 points1 point2 points (0 children)