Python Forum
I hate "List index out of range"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I hate "List index out of range"
#8
(May-09-2023, 09:44 PM)Melen Wrote: I can't understand the logic of the "index out of range" error

What specifically is beyond your understanding? If you count all items in object starting from zero and get 0, 1, 2 then what should happen if you are looking for item 4? To put it another way - if you have deck of 5 cards what should you say (or do) if somebody asks to give 6th card in the deck?

Slices can be out of range without raising error:

>>> s = 'abc'
>>> len(s)
3
>>> s[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[3:]
''
>>> s[100:120]
''
>>> l = [1, 2, 3]
>>> l[3:]
[]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
I hate "List index out of range" - by Melen - May-09-2023, 09:44 PM
RE: I hate "List index out of range" - by bowlofred - May-10-2023, 02:56 AM
RE: I hate "List index out of range" - by Melen - May-10-2023, 09:34 PM
RE: I hate "List index out of range" - by Melen - May-10-2023, 09:40 PM
RE: I hate "List index out of range" - by ibreeden - May-11-2023, 09:38 AM
RE: I hate "List index out of range" - by perfringo - May-11-2023, 01:10 PM
RE: I hate "List index out of range" - by DeaD_EyE - May-11-2023, 03:58 PM
RE: I hate "List index out of range" - by Melen - May-11-2023, 09:09 PM
RE: I hate "List index out of range" - by Melen - May-12-2023, 05:17 AM
RE: I hate "List index out of range" - by Melen - May-12-2023, 08:42 PM
RE: I hate "List index out of range" - by Melen - May-13-2023, 11:17 AM
RE: I hate "List index out of range" - by Melen - May-13-2023, 01:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 7,034 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 2,077 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,533 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,542 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,826 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,397 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 3,013 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,468 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,291 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,887 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020