33
u/JestemStefan Aug 17 '25
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.
4
7
Aug 17 '25
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)
1
3
u/earchip94 Aug 17 '25 edited Aug 18 '25
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.
2
u/AwkwardBet5632 Aug 17 '25
range(n) goes from 0 to n-1.
2
u/earchip94 Aug 17 '25
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.
2
u/HotLaMon Aug 17 '25
range(n)
What is n?
n = len(l)
What is the length of l? 5
n = 5print(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.
0
u/AwkwardBet5632 Aug 17 '25
Yes, and OP’s error has nothing to do with the comment I was replying to, which it claiming a problem with the range.
2
u/earchip94 Aug 18 '25
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.
3
2
u/denehoffman Aug 17 '25
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.
9
u/coopsoup247 Aug 17 '25 edited Aug 17 '25
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
2
u/ilidan-85 Aug 17 '25
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!
2
u/coopsoup247 Aug 17 '25
My bad. I wrote it on my phone and it didn't format correctly. I've fixed it now.
1
u/therouterguy Aug 17 '25
What is not readable about this?
1
u/coopsoup247 Aug 17 '25
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.
1
1
u/purple_hamster66 Aug 17 '25
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.
1
u/Sea_Sir7715 Aug 17 '25
l= [9, 2, 3, 6, 5] # you are defining the list “l”
n= len(l) # It is 5, gives the value of the length of the list
for i in range (n): #here you ask Python to create a sequence of numbers n-1 (5-1=4) that if you printed just this line, it would look like this:
0 1 2 3 4
print(l[n]) # you use slicing to extract the position l[n] from your list, however, that value is outside because the positions start from 0, which is the index, to 4, which is the last position. Another way to get to the last position is l[-1] or l[4]
1
1
1
1
u/DevRetroGames Aug 17 '25
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.
1
1
u/Past-Cucumber-3536 Aug 17 '25
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:
1
1
1
u/Lobotomized_toddler Aug 18 '25
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
1
1
1
u/Wrong-Bit8534 Aug 17 '25
Array starts from 0, try n-1
4
u/AwkwardBet5632 Aug 17 '25
range(n) goes from 0 to n-1.
1
u/Swipsi Aug 17 '25
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.
1
1
u/Wrong-Bit8534 Aug 17 '25
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
0
42
u/Grounds4TheSubstain Aug 17 '25
Your print statement says n, not i.