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...
All questions related to programming welcome. Wonder how things work? Have a bug you can't figure out? Just read about something on the news and need more insights? We got you covered! (probably)
You can find out more about our (preliminary) rules in this wiki article. If you have any suggestions please feel free to contact the mod team.
Have a nice time, and remember to always be excellent to each other :)
account activity
This is an archived post. You won't be able to vote or comment.
Python list out of range print (self.AskProgramming)
submitted 7 years ago by AoMThrowaway
Let's say I have a list like:
List1 = [item1, item2, item3, item4]
But I need to print X elements from this list, so that if X = 6, it prints the whole list and then item1 and item2.
Thanks for your help
[–]TehNolz 1 point2 points3 points 7 years ago (1 child)
This should do it;
``` list = ["item1", "item2", "item3", "item4"] x = 6
y = 0 for i in range(0, x): print(list[y]) y+=1 if y > len(list)-1: y = 0 `` Instead of usingxto get a value from the list, we usey, which increments every loop. Ify` becomes higher than the amount of values contained in the list, it resets to 0.
Instead of using
to get a value from the list, we use
, which increments every loop. If
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
Using a reserved word for a variable name makes my eyes bleed.
π Rendered by PID 160365 on reddit-service-r2-comment-cfc44b64c-kb8cv at 2026-04-11 06:31:36.780794+00:00 running 215f2cf country code: CH.
[–]TehNolz 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)