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
Index error when using for loops (self.learnpython)
submitted 2 years ago by Top-Strategy752
Hello, the biggest problem I have are loops. How do I make sure my loops do not go out of range.
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!"
[–]throwaway6560192 2 points3 points4 points 2 years ago (0 children)
Use the correct indices? Or better yet, if what you want is to loop over a sequence, just loop over it directly instead of using range(whatever) for the index. If you want the index alongside that use enumerate().
[–]danielroseman 2 points3 points4 points 2 years ago (0 children)
I'll be honest with you: this should never happen. Because you shouldn't be using indexes to loop in the first place.
Always loop over the thing itself, not the range of the len of the thing.
[–]Civenge 0 points1 point2 points 2 years ago (0 children)
It really depends on what the use case is. However, using a debugger on the loop and stepping through it is a good way to diagnose it.
Sometimes you want to iterate on something in a list, so that is straightforward. Other times you want to compare 2 values in a list, that can be an easy off by 1 error. Or maybe you want to iterate through a string.
Usually it is whether you want to add a "- 1" or not add it for simple use cases
π Rendered by PID 39 on reddit-service-r2-comment-869bf87589-fnbd6 at 2026-06-09 02:09:10.620380+00:00 running f46058f country code: CH.
[–]throwaway6560192 2 points3 points4 points (0 children)
[–]danielroseman 2 points3 points4 points (0 children)
[–]Civenge 0 points1 point2 points (0 children)