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
#1
I understand how to use a for loop with range(len(list)) to be able to cycle through and address each item in a list.
However (and in my case a directory listing), I'm trying to figure out how to provide (print in this case) the next 5 in the list. However when the list is down to the last 3 --and not 5, I get list-index-o-o-r; which makes sense, but i'm not sure how to approach this without a whole lot of "if/then/elif" etc.

So if I have 13 files, I want to print the first 5, ask a question (continue?) and present the next 5, etc, and then present onyly the last 3

..so when it's 13 items, I'll obviously get the l-i-o-o-r error;

Is there an "elegant" way, or a built in way to do this... or do I need to implement a modulus (%5) to see if there are additional files; and also validate i'm not at end of list, etc.

import os
temp_dir = os.listdir("c:\\temp")

'''
# This works no prob
for item in temp_dir:
	print (item)
	answer = input('Continue? y/n')
	if answer == 'n':
		break
'''
for by5 in range(0,len(temp_dir),5):
	print (temp_dir[by5])
	print (temp_dir[by5+1])
	print (temp_dir[by5+2])
	print (temp_dir[by5+3])
	print (temp_dir[by5+4])
Thanks, banging my head over the weekend but felt like there might be a "c'mon old man, use function widget to do that"

Pappa Bear

Well, it appears that yield may be what I was looking for... still not sure if this is the best method -- essentially breaks the list into n chunks, which are in a list... [ [1a 1b 1c 1d 1e] [2a 2b 2c 2d 2e] [3a 3b] ] giving me 3chunks of 5 (okay, 2 chunks of 5 and 1 chunk of remainder)

def into_chunks(list, size): 
    for counter in range(0, len(list), size):  
        yield list[counter:counter + size] 

chunk_size = 5
temp_dir = os.listdir("c:\\temp")
dir_list = list(into_chunks(temp_dir, chunk_size)) 

for group in dir_list:
	for file in group:
		print (file)
	print ("Continue? ")
Thoughts on this being the best method? Right now I'm simply printing continue...but will be implementing a crude menu system (select file by 'a/b/c/d/e/next')

Thanks,
Pappa Bear
Reply


Messages In This Thread
Preventing: IndexError: list index out of range - by PappaBear - Jun-03-2019, 04:57 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,199 Sep-14-2023, 06:54 AM
Last Post: Mehboob
Thumbs Down I hate "List index out of range" Melen 20 3,396 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,507 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,956 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,386 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,470 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,622 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  Preventing Duplicate Placement in 2D Array nickdavis2017 2 1,627 Feb-03-2022, 11:06 PM
Last Post: nickdavis2017
  matplotlib x axis range goes over the set range Pedroski55 5 3,268 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,888 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