Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index out of range error
#1
Hi Everyone,
I am trying to create a sublist which uses a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).
I am able to acheive that using the below code but getting Index Error: List index out of range.

def sublist(l):
my_list= []
idx= 0
while l[idx]!= 5:
my_list.append(l[idx])
idx= idx+1
return my_list
p= [3,4,2,43,5,6]
print(sublist(p))
I appreciate any help on this.

Thanks,
Shikha
Reply
#2
First - this code runs ok on my machine if intentation is fixed.

>>> def sublist(l):
...     my_list= []
...     idx= 0
...     while l[idx]!= 5:
...         my_list.append(l[idx])
...         idx= idx+1
...     return my_list
...
>>> p = [3,4,2,43,5,6]
>>> print(sublist(p))
[3, 4, 2, 43]
However, this is very complicated solution. You know the number which should break the original list to sublist. You can get index of that number and make slice:

>>> new = p[:p.index(5)]
>>> new
[3, 4, 2, 43]
If there is no 5 in the list then ValueError will be raised. If there is possibility that it could happen then it should be wrapped into try..except.

EDIT: now I get it. IndexError will be raised when there is no 5 in list. It logical as index is incremented until 5 is encountered.

You should use for loop instead of while:

>>> def sublist(l):
...     my_list = []
...     for el in l:
...         if el == 5:
...             break
...         else:
...             my_list.append(el)
...     return my_list
...
>>> p = [3,4,2,43,6]
>>> sublist(p)
[3, 4, 2, 43, 6]
>>> p = [3,4,2,43,5,6]
>>> sublist(p)
[3, 4, 2, 43]
With index and slicing it could be:

>>> def sublist(l, *, breaker=5):
...     try:
...         return l[:l.index(breaker)]
...     except ValueError:
...         return l[:]                   
...
>>> p = [3,4,2,43,6]
>>> sublist(p)
[3, 4, 2, 43, 6]
>>> p = [3,4,2,43,5, 6]
>>> sublist(p)
[3, 4, 2, 43]
>>> sublist(p, breaker=2):
[3, 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
Taking of advantage built-in iter() function and combining it with functools.partial one can extract sublist with unreadable oneliner:

>>> from functools import partial
>>> p = [3, 4, 2, 43, 5, 6]
>>> list(iter(partial(next, iter(p)), 5))
[3, 4, 2, 43]
Slightly more readable as function:

>>> from functools import partial
>>> def sublist(l, *, breaker=5):
...     give_me_next_in_list = partial(next, iter(l))
...     return list(iter(give_me_next_in_list, breaker))
...
>>> sublist([3, 4, 2, 43, 5, 6])
[3, 4, 2, 43]
>>> sublist([3, 4, 2, 43, 6])
[3, 4, 2, 43, 6]
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range rf_kartal 6 379 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 310 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 730 Aug-09-2021, 04:46 PM
Last Post: snippsat
  IndexError: list index out of range Laplace12 1 525 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  python error: bad character range \|-t at position 12 faustineaiden 0 500 May-28-2021, 09:38 AM
Last Post: faustineaiden
  How to resolve Index Error in my code? codify110 6 816 May-22-2021, 11:04 AM
Last Post: supuflounder
  IndexError: list index out of range brunolelli 11 1,723 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 620 Mar-25-2021, 07:20 PM
Last Post: buran
  IndexError: list index out of range ramu4651 2 1,055 Jan-24-2021, 01:45 PM
Last Post: buran
  string index out of range jade_kim 4 833 Jan-13-2021, 05:41 AM
Last Post: jade_kim

Forum Jump:

User Panel Messages

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