Python Forum
catching / handle index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
catching / handle index out of range
#1
Guys,

Back again, fiddling with an 'index out of range' thingy. To my (limited) knowledge this is how I would do it.

Your thoughts?
a = [1,2,3]
b = [1]
c = [1,4,5,6]
d = [1,2,3]

e = [a,b,c,d]
index_needed = 2
output = [item[index_needed] for item in e if len(item) > index_needed]

print(output)
# outputs 3, 5, 3
Reply
#2
Is it output you expected or there is problem with that?

There might be times when you need to get some value in cases where index is out of range. To achieve that you don't filter out indexes which are out of range but with conditional expression you return appropriate value:

>>> index_needed = 3
>>> e = [[1, 2], [1, 2, 3, 4], [1], [1, 2, 3, 4, 5]]
>>> [item[index_needed] if len(item) > index_needed else None for item in e]
[None, 4, None, 4]
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
#3
Hmm I thought I was satisfied with my setup, but looking at your suggestion: including a None and thus keeping the list length the same might be better actually. And perhaps better in terms of a clear output.

Good one!
Reply
#4
you can take advantage of itertools.zip_longest()

from itertools import zip_longest

a = [1,2,3]
b = [1]
c = [1,4,5,6]
d = [1,2,3]
 
index_needed = 2
combined = list(zip_longest(a,b,c,d))
try:
    print(combined[index_needed])
except IndexError:
    print(f'There is no element with index {index_needed}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,979 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,847 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,411 May-03-2022, 01:39 PM
Last Post: Anldra12
  Catching a crash within a library code ebolisa 9 3,069 Nov-22-2021, 11:02 AM
Last Post: bowlofred
  matplotlib x axis range goes over the set range Pedroski55 5 3,112 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,764 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,186 Jun-22-2021, 10:47 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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