Python Forum
Preventing: IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Preventing: IndexError: list index out of range
#2
Use a try and except
for by5 in range(0,len(temp_dir),5):
    try:
        print (temp_dir[by5])
        print (temp_dir[by5+1])
        print (temp_dir[by5+2])
        print (temp_dir[by5+3])
        print (temp_dir[by5+4])
    except:
        print('That\'s all')

So you try to print everything, except if you get an error it is probably the index error meaning you are done.
Reply


Messages In This Thread
RE: Preventing: IndexError: list index out of range - by SheeppOSU - Jun-03-2019, 05:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 10 is out of bounds for axis 0 with size 10 Mehboob 11 2,104 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,319 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,361 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,913 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,349 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,442 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,581 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Preventing Duplicate Placement in 2D Array nickdavis2017 2 1,586 Feb-03-2022, 11:06 PM
Last Post: nickdavis2017
  matplotlib x axis range goes over the set range Pedroski55 5 3,209 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,847 Sep-07-2021, 02:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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