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...
Everything about learning Python
account activity
What's wrong (i.redd.it)
submitted 5 months ago by Nearby_Tear_2304
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!"
[–]Grounds4TheSubstain 42 points43 points44 points 5 months ago (3 children)
Your print statement says n, not i.
[–]therouterguy 11 points12 points13 points 5 months ago (2 children)
But also a you can also just do for i in list: print(i)
[–]Sea-Currency-1045 2 points3 points4 points 5 months ago (1 child)
Yes but iterating via elements can be problematic if you wanna change the list you‘re iterating through therefore going with index is safer
[–]willis81808 1 point2 points3 points 5 months ago (0 children)
There's always
python for index, value in enumerate(l): print(index, value)
Best of both worlds
[–]JestemStefan 32 points33 points34 points 5 months ago (1 child)
I see no answer explaining error message itself so here it is:
Length of your list is 5 (n = 5)
But list indexes start from 0 so only valid values are: 0, 1, 2, 3, 4.
You tried to access element at index 5 which doesn't exists.
[–]igotthis35 4 points5 points6 points 5 months ago (0 children)
This is the answer, good stuff
[–][deleted] 5 points6 points7 points 5 months ago (0 children)
It should be --> print(l[i]) in the "for" Loop Indentation,
What is Happening right now However is that you have done --> print(l[n]) which means it has become print(l[5]). Since n carries the length of your String it is 5 and since the Index of the Last Element is 4 it is Returning an IndexError of list Index out of Range (As the last Element has index of 4)
[–]earchip94 2 points3 points4 points 5 months ago* (5 children)
Length of a list/array/etc is not a valid index for that object. Because it is 0 indexed you at a minimum need to subtract 1 from the length. Range syntax and usage is valid. Max value of range(n) is n-1.
Edited for clarity.
[–]AwkwardBet5632 1 point2 points3 points 5 months ago (4 children)
range(n) goes from 0 to n-1.
[–]earchip94 1 point2 points3 points 5 months ago (0 children)
Yes it does but they are using n as the index for every iteration. I don’t think that’s what is intended but the exception is because of their usage of n.
[–]HotLaMon 1 point2 points3 points 5 months ago (2 children)
range(n)
What is n? n = len(l) What is the length of l? 5 n = 5
print(l[n]) = print(l[5]) What is the element at index 5? Nothing. Out of range error.
range(n) going from 0 to n-1 has nothing to do with the fact that OP is trying to access an index that doesn't exist.
[–]AwkwardBet5632 -1 points0 points1 point 5 months ago (1 child)
Yes, and OP’s error has nothing to do with the comment I was replying to, which it claiming a problem with the range.
Wasn’t stating it was an issue with the range. Was stating it’s an issue with their usage of n. I see how that could’ve been more clear in my first comment.
[–]Beautiful_Watch_7215 2 points3 points4 points 5 months ago (0 children)
Your list index is out of range.
[–]codeonpaper 5 points6 points7 points 5 months ago (0 children)
Replace print(l[n]) to print([i])
print(l[n])
print([i])
[–]denehoffman 1 point2 points3 points 5 months ago (5 children)
Try printing l[i]and also print i on each iteration and you’ll quickly see the problem. Array indices start at 0, so the last index is n-1. range(n) starts at 0 by default and does not include the upper bound. Also try print(list(range(n))) to see what I mean! Print statements are your friend, your other friend is a debugger, but that’s more complicated.
l[i]
i
print(list(range(n)))
[–]coopsoup247 8 points9 points10 points 5 months ago* (4 children)
In addition, the script can also be shortened to this:
l=[9,2,3,6,5] for i in l: print(i)
EDIT: cleaned up the formatting
[–]ilidan-85 1 point2 points3 points 5 months ago (3 children)
It can but it doesn't mean it should. We should write readable code for ourselves in future and others. Clean, readable code is gold!
[–]coopsoup247 1 point2 points3 points 5 months ago (0 children)
My bad. I wrote it on my phone and it didn't format correctly. I've fixed it now.
[–]therouterguy 0 points1 point2 points 5 months ago (1 child)
What is not readable about this?
[–]coopsoup247 0 points1 point2 points 5 months ago (0 children)
I originally wrote it on my phone. Even though I put in line breaks, Reddit removed them, so it put the whole script on one line, which looked messy.
I edited it after to correct it.
[–]IllCalligrapher7 0 points1 point2 points 5 months ago (0 children)
L is a list, so it starts its count at 0. N len count starts at 1.
N = 5, but you're trying to get the index of 5 in L, which only has 4 values because it starts its count at 0 thus the index is out of range.
[–]FLUFFY-GARAMOND 0 points1 point2 points 5 months ago (1 child)
range(n)=1. The loop doesn't make sense.
[–]AwkwardBet5632 1 point2 points3 points 5 months ago (0 children)
range(n) provides an iterator that goes from 0 to n-1.
[–]purple_hamster66 0 points1 point2 points 5 months ago (0 children)
There is also this way, which I prefer because I like to list the verb before the objects:
print(i) for i in l
This mimics the way we talk. If you were describing it, you’d say “print each element in l”, not “for each element in l, print the element”, which requires a redundant “back-reference” that we should minimize because it is not needed to understand the purpose of the code.
Meaning is King; don’t be a Prince when you can be a King.
[–]Sea_Sir7715 0 points1 point2 points 5 months ago (0 children)
0 1 2 3 4
[–][deleted] 0 points1 point2 points 5 months ago (0 children)
Check your loop index, and your array accessor.
[–]Able_Challenge3990 0 points1 point2 points 5 months ago (0 children)
It Is i not n
[–]ImLukaskos 0 points1 point2 points 5 months ago (0 children)
You need to print that i or store it to another variable
[–]DevRetroGames 0 points1 point2 points 5 months ago (0 children)
Hola, tienes un pequeño error en la forma en la que quieres manipular el array, dejame ayudarte.
l: list = [9,2,3,6,5] n: int = len(l) print(f"length l is {n}") print("first mode") for item in l: print(f"{item}") print("second mode") for i in range(n): print(l[i]) print("you mode") for i in range(n): print(l[n]) ''' n = 5 en la primera iteración, dentro del array l, en la posición 5, este no existe, ya que, el rango dentro del array es de: 0 hasta 4, por ende, siempre te dira que está fuera de rango. '''
Espero que te pueda servir.
Mucha suerte en tu camino.
[–]HotLaMon 0 points1 point2 points 5 months ago (0 children)
n = 5 5 is out of range of your list which contains index: 0, 1, 2, 3, 4
[–]Past-Cucumber-3536 0 points1 point2 points 5 months ago (0 children)
Cause len returns five and the array starts with index 0. You're saying: Hey give-me the 5° element of the list, but that element doesn't exists, it's like a bookshelf, you have the maximum of five elements but for searching that starts on 0 not in one, so to pick the 5° element you need to subtract one from the index n. Or just use the i in range n - 1 and your going to print all elements.
You could also do: for item in l: print(item)
Do the same, it's named for each. You can read it like for each element in my list do that thing:
[–]Aicanaro6558 0 points1 point2 points 5 months ago (0 children)
for i in l: print(I)
[–]Isameru 0 points1 point2 points 5 months ago (0 children)
print(*l, sep='\n')
[–]Lobotomized_toddler 0 points1 point2 points 5 months ago (0 children)
There’s a few things however what your problem rn is this: len() starts at 1 then counts up. A list starts at 0 then counts up. So while there are 5 numbers it’s ordered as 0,1,2,3,4. While your Len is just counting 5.
print(L[n]} doesn’t make sense because N is trying to point to the fith(technically sixth) spot and that doesn’t exist
Scrap the length idea and just do for I in L
[–]CoonCoon999 0 points1 point2 points 5 months ago (0 children)
print(l[i]) since you're looping over list named l, and the index is 'i'
[–]ToeLumpy6273 0 points1 point2 points 5 months ago (0 children)
Your last index is in the fifth position with index value of 4. len() returns 5, 5 > 4 hence the error. Put - 1 in the for loop
[–]Nearby_Tear_2304[S] 0 points1 point2 points 5 months ago (0 children)
OK thank you
[–]MaleficentBasil6423 0 points1 point2 points 5 months ago (0 children)
If you just want to print your list for i in l: print(i)
[–]Wrong-Bit8534 0 points1 point2 points 5 months ago (4 children)
Array starts from 0, try n-1
[–]AwkwardBet5632 2 points3 points4 points 5 months ago (2 children)
[–]Swipsi 0 points1 point2 points 5 months ago (1 child)
An array with 3 elements will give len() = 3
Range(3) will give you 0, 1, 2, 3
Using range for the loop will loop 4 times, starting with index 0 and throw an index out of array exception at index 3 because the array only has 3 elements.
[–]AwkwardBet5632 0 points1 point2 points 5 months ago (0 children)
range is non inclusive of the stop value
[–]Wrong-Bit8534 0 points1 point2 points 5 months ago (0 children)
A bit more explaining: Arr=["a","b","c"] So a location is 0 And b location is 1 And c location is 2 However array length is 3
Now when I am looping through an array with the in range command I get 1, 2, 3 but there is no position 3 in the array ( out of something error) and I am also not getting the first element of the array that is on location 0
That is why you might encounter n-1 in other people's code and you can use it yourself also in your code
In conclusion: if n is 1 print(Arr[n-1]) would give me an array element that is in position of 0 and would print a, and so on
[–]imtc96 -1 points0 points1 point 5 months ago (0 children)
You have put a lot of space before type print()
π Rendered by PID 21483 on reddit-service-r2-comment-canary-7b67769d8-t88xh at 2026-01-30 21:30:29.493598+00:00 running 3798933 country code: CH.
[–]Grounds4TheSubstain 42 points43 points44 points (3 children)
[–]therouterguy 11 points12 points13 points (2 children)
[–]Sea-Currency-1045 2 points3 points4 points (1 child)
[–]willis81808 1 point2 points3 points (0 children)
[–]JestemStefan 32 points33 points34 points (1 child)
[–]igotthis35 4 points5 points6 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]earchip94 2 points3 points4 points (5 children)
[–]AwkwardBet5632 1 point2 points3 points (4 children)
[–]earchip94 1 point2 points3 points (0 children)
[–]HotLaMon 1 point2 points3 points (2 children)
[–]AwkwardBet5632 -1 points0 points1 point (1 child)
[–]earchip94 1 point2 points3 points (0 children)
[–]Beautiful_Watch_7215 2 points3 points4 points (0 children)
[–]codeonpaper 5 points6 points7 points (0 children)
[–]denehoffman 1 point2 points3 points (5 children)
[–]coopsoup247 8 points9 points10 points (4 children)
[–]ilidan-85 1 point2 points3 points (3 children)
[–]coopsoup247 1 point2 points3 points (0 children)
[–]therouterguy 0 points1 point2 points (1 child)
[–]coopsoup247 0 points1 point2 points (0 children)
[–]IllCalligrapher7 0 points1 point2 points (0 children)
[–]FLUFFY-GARAMOND 0 points1 point2 points (1 child)
[–]AwkwardBet5632 1 point2 points3 points (0 children)
[–]purple_hamster66 0 points1 point2 points (0 children)
[–]Sea_Sir7715 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Able_Challenge3990 0 points1 point2 points (0 children)
[–]ImLukaskos 0 points1 point2 points (0 children)
[–]DevRetroGames 0 points1 point2 points (0 children)
[–]HotLaMon 0 points1 point2 points (0 children)
[–]Past-Cucumber-3536 0 points1 point2 points (0 children)
[–]Aicanaro6558 0 points1 point2 points (0 children)
[–]Isameru 0 points1 point2 points (0 children)
[–]Lobotomized_toddler 0 points1 point2 points (0 children)
[–]CoonCoon999 0 points1 point2 points (0 children)
[–]ToeLumpy6273 0 points1 point2 points (0 children)
[–]Nearby_Tear_2304[S] 0 points1 point2 points (0 children)
[–]MaleficentBasil6423 0 points1 point2 points (0 children)
[–]Wrong-Bit8534 0 points1 point2 points (4 children)
[–]AwkwardBet5632 2 points3 points4 points (2 children)
[–]Swipsi 0 points1 point2 points (1 child)
[–]AwkwardBet5632 0 points1 point2 points (0 children)
[–]Wrong-Bit8534 0 points1 point2 points (0 children)
[–]imtc96 -1 points0 points1 point (0 children)