Bottom Page

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
Quote
#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.
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  item = index position - list of list RavCOder 9 162 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  IndexError: List index out of range issue Adem 1 327 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 325 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba
  returns index of list if contains a word zarize 0 152 Sep-09-2019, 09:29 AM
Last Post: zarize
  MAWS.py PRGM:IndexError: list index out of range bcramer 0 154 Aug-28-2019, 04:45 PM
Last Post: bcramer
  Select a part of an element of a list with Index BollerwagenIng 0 305 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng
  IndexError: string index out of range ? Q_Y 5 912 Jul-05-2019, 07:18 PM
Last Post: noisefloor
  index out of range rwahdan 3 566 Jul-04-2019, 08:11 PM
Last Post: ichabod801
  List of dictionaries: use key-value instead of index? dn237 6 576 Jun-03-2019, 05:52 PM
Last Post: Yoriz
  json.load causing IndexError: list index out of range WallyZ 5 790 May-20-2019, 01:11 PM
Last Post: WallyZ

Forum Jump:


Users browsing this thread: 1 Guest(s)