Questions tagged [index-error]

An `IndexError` indicates that an out of bounds or invalid index was used.

Python

The IndexError is raised only when the index is out of bounds:

>>> a = [1, 2, 3, 4, 5]
>>> a[10]
IndexError                                Traceback (most recent call last)
[...]
IndexError: list index out of range

However invalid entries will raise a .

Ruby

See their IndexError documentation for an example which raises an IndexError.

640 questions
140
votes
9 answers

Return string with first match for a regex, handling case where there is no match

I want to get the first match of a regex. In the following case, I have a list: text = 'aa33bbb44' re.findall('\d+',text) # ['33', '44'] I could extract the first element of the list: text = 'aa33bbb44' re.findall('\d+',text)[0] # '33' But that…
Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181
53
votes
7 answers

Does "IndexError: list index out of range" when trying to access the N'th item mean that my list has less than N items?

I'm telling my program to print out line 53 of an output. Is this error telling me that there aren't that many lines and therefore can not print it out?
Tyler
  • 3,919
  • 7
  • 27
  • 26
47
votes
4 answers

What does 'index 0 is out of bounds for axis 0 with size 0' mean?

I am new to both python and numpy. I ran a code that I wrote and I am getting this message: 'index 0 is out of bounds for axis 0 with size 0' Without the context, I just want to figure out what this means.. It might be silly to ask this but what do…
Seoyeon Hong
  • 553
  • 2
  • 5
  • 8
38
votes
3 answers

Why do I get an IndexError (or TypeError, or just wrong results) from "ar[i]" inside "for i in ar"?

I'm trying to sum the values of a list using a for loop. This is my code: def sumAnArray(ar): theSum = 0 for i in ar: theSum = theSum + ar[i] return theSum I get the following error: line 13, theSum = theSum + ar[i] IndexError:…
q-compute
  • 641
  • 2
  • 7
  • 15
25
votes
5 answers

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "", line 1, in t3 =…
ricky_hehe
  • 295
  • 1
  • 3
  • 5
20
votes
5 answers

IndexError: tuple index out of range when I try to create an executable from a python script using auto-py-to-exe

I have been trying out an open-sourced personal AI assistant script. The script works fine but I want to create an executable so that I can gift the executable to one of my friends. However, when I try to create the executable using the…
DevLearner
  • 369
  • 3
  • 11
14
votes
2 answers

IndexError: Replacement index 1 out of range for positional args tuple

I am following a tutorial and I don't know why I got this error: in extract_featuresets(ticker) 2 tickers, df = process_data_for_labels(ticker) 3 df['{}_target'.format(ticker)] =…
balexander1
  • 182
  • 1
  • 1
  • 8
9
votes
5 answers

How can I find out which index is out of range?

In the event of an IndexError, is there a way to tell which object on a line is 'out of range'? Consider this code: a = [1,2,3] b = [1,2,3] x, y = get_values_from_somewhere() try: a[x] = b[y] except IndexError as e: .... In the event that x…
David Board
  • 349
  • 3
  • 12
5
votes
3 answers

IndexError: list assignment index out of range Python 3

Someone has a Idea why i get an IndexError in this code? global gegner global gegnerhp gegner = [] gegberhp = [] for i in range(1,anzahlgegner): random = randint(1,5) …
zwerg4
  • 320
  • 3
  • 5
  • 12
4
votes
1 answer

IndexError: Dimension out of range - PyTorch dimension expected to be in range of [-1, 0], but got 1

Despite already numerous answers on this very topic, failing to see in the example below (extract from https://gist.github.com/lirnli/c16ef186c75588e705d9864fb816a13c on Variational Recurrent Networks) which input and output dimensions trigger the…
iris
  • 379
  • 1
  • 2
  • 9
4
votes
3 answers

I got error saying IndexError: list index out of range. how do I fix this?

res = [3, 1, 1, 5, 2, 4, 2, 4, 2, 4, 3, 1, 1, 5, 3] while not i>(len(res)-1): if res[i]==res[i+1]: answer+=2 i+=2 else: i+=1 The variable "answer" is supposed to count duplicated…
4
votes
5 answers

Python string recursion, string index out of range

I am new at python and very bad at thinking in a recursive way. This code gives me an IndexError: string index out of range. And I have no idea how to correct it. def get_permutations(sequence): def permutationhelp(sequence,chosen): if…
Em Rul
  • 57
  • 5
4
votes
2 answers

Weird "too many indices for array" error in python

Let's create a large np array 'a' with 10,000 entries import numpy as np a = np.arange(0, 10000) Let's slice the array with 'n' indices 0->9, 1->10, 2->11, etc. n = 32 b = list(map(lambda x:np.arange(x, x+10), np.arange(0, n))) c = a[b] The weird…
4
votes
1 answer

TFlearn IndexError out of bounds

I have some data which looks like this: X = [[1,2,3,4],[01010],[-1.6]] y = [[4,2]] I am trying to train a neural net on this data using tflearn. I'm using the same example given on the TFlearn github homepage (https://github.com/tflearn/tflearn)…
HS1300
  • 59
  • 1
  • 10
4
votes
1 answer

"IndexError: list index out of range" When trying to load weights using keras' vgg16

I want to load a set of pre-trained weights downloaded from this address: https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3 I am using the code below to load the weights: import os from PIL import Image import tensorflow from…
ArsonFG
  • 315
  • 8
  • 22
1
2 3
42 43