TypeError: List Indices Must Be Integers Or Slices, Not Tuple

Estimated reading time: 3 minutes

When working with Python lists in your data analytics projects, when you trying to read the data, a common problem occurs if you have a list of lists, and it is not properly formatted.

In this instance, Python will not be able to read one or more lists and as a result, will throw this error.

In order to understand how this problem occurs, we need to understand how to create a list of lists.

How to create a lists of lists

Let’s look at a simple list:

a = [1,2,3]
print(a)
print(type(a))

Result:
[1, 2, 3]
<class 'list'>

Let’s create a second list called b:

b = [4,5,6]
print(b)
print(type(b))

Result:
[4, 5, 6]
<class 'list'>

So if we want to join the lists together into one list ( hence a list of lists) then:

a = [1,2,3]
b = [4,5,6]

list_of_lists = []
list_of_lists.append(a)
list_of_lists.append(b)
print(list_of_lists)
print(type(list_of_lists))

Result:
[[1, 2, 3], [4, 5, 6]]
<class 'list'>

So as can be seen the two lists are contained within a master list called “list_of_lists”.

So why does the error list indices must be integers or slices, not tuple occur?

Reason 1 ( Missing commas between lists)

If you manually type them in and forget the comma between the lists this will cause your problem:

a=[[1,2,3][4,5,6]]
print(a)

Result (Error):
Traceback (most recent call last):
  line 10, in <module>
    a=[[1,2,3][4,5,6]]
TypeError: list indices must be integers or slices, not tuple

But if you put a comma between the two lists then it returns no error:

a=[[1,2,3],[4,5,6]]
print(a)

Result (no error):
[[1, 2, 3], [4, 5, 6]]
Process finished with exit code 0

Reason 2 ( Comma in the wrong place)

Sometimes you have a list, and you only want to bring back some elements of the list, but not others:

In the below, we are trying to bring back the first two elements of the list.

a= [1,2,3,4,5]
print(a[0:,2])

Result:
Traceback (most recent call last):
   line 14, in <module>
    print(a[0:,2])
TypeError: list indices must be integers or slices, not tuple

The reason that the same error happens is the additional comma in a[0:,2], causes the error to appear as Python does not know how to process it.

This is easily fixed by removing the additional comma as follows:

a= [1,2,3,4,5]
print(a[0:2])

Result:
[1, 2]
Process finished with exit code 0

So why is there a mention of tuples in the error output?

The final piece of the jigsaw needs to understand why there is a reference to a tuple in the error output?

If we return to a looking at a list of lists and look at their index values:

a=[[1,2,3],[4,5,6]]
z = [index for index, value in enumerate(a)]
print(z)

Result:
[0, 1]
Process finished with exit code 0

As can be seen, the index values are 0,1, which is correct.

As before removing the comma gives the error we are trying to solve for:

a=[[1,2,3][4,5,6]]
z = [index for index, value in enumerate(a)]
print(z)

Result:
Traceback (most recent call last):
  line 16, in <module>
    a=[[1,2,3][4,5,6]]
TypeError: list indices must be integers or slices, not tuple

BUT the reference to the tuple comes from the fact that when you pass two arguments (say an index value) a Tuple is created, but in this instance, as the comma is missing the tuple is not created and the error is called.

This stems from the __getitem__ for a list built-in class cannot deal with tuple arguments that are not integers ( i.e. 0,1 like we have returned here) , so the error is thrown, it is looking for the two index values to pass into a tuple.

raise an exception in python

Estimated reading time: 5 minutes

In a computer programme, there is a lot of different moving parts, that the programmer has created themselves.

And depending on the sophistication of the code written exceptions can occur, usually no one has an understanding of what they mean!

So we have established problems can occur, what next?

As part of the process of building out the logic , knowledge will lie with the computer programmer.

In this case the errors that they know about and programme for, may not always be understood by everyone.

If the computer programme does not explain them correctly, frequently it can lead to long hours trying to figure out the problem.

This is usually because the error message they code to explain the problem, may not always be designed with ease of understandability.

It must be remembered:

  • Not all errors may be known when the programme was developed.
  • Some error messages can be generated automatically, not on the request of the programmer.

Step into the frame, the ability to create your own error messages with understandable explanations.

Good programmers have a lot of traits that makes what they develop so powerful.

Surprisingly it is the simple things can be missed, but based on time and budget, generic error messages may be needed, certainly not ideal though.

With that in mind lets look at the following code:

def checkvalue():
    try:
        integervalues = input("Type something to test this out: ")
        if integervalue != "55" and integervalue.isdecimal():
            raise ValueError
        elif integervalue.isalpha():
            raise TypeError
        else:
            print("55 entered correctly")
    except ValueError:
        raise ValueError("Integervalue needs to be fifty five")
    except TypeError:
        raise TypeError("Number needs to be entered")
    finally:
        print("All checks completed")


checkvalue()
It returns:
NameError: name 'integervalue' is not defined

The problem is that the variable used has a misspelling.

Luckily we can easily see that the variable “integervalue” is misspelt with an extra “s”, however removing that will fix the issue.

In the light of that say we took the above code, without the try and except:

from distlib.compat import raw_input
def checkvalue():
    integervalue =raw_input("Type something to test this out: ")

    if integervalue == int(55):
        print("success")
    else:
        print("integervalue needs to be a fifty five")


checkvalue()
It returns:
Type something to test this out: aa
integervalue needs to be a fifty five

In essence, both do the same thing, above all the first piece of code allows for better customization.

This allows us to test for specific types of errors, resulting in more specific error trapping like:

  • Value Error
  • Type Error

The above two are just examples you can use for built in exceptions coded to trap errors.

But what if we want to create our own exception list?

The simple way around this is to build your own class that has the errors you need to capture.

Then you reference that class in the try/except and bingo, there you have it.

In order to achieve this, the first step is to build classes with the errors you want to catch:

class ValueTooSmall(Exception):
    """Raised when the input value is too small"""
    pass
class ValueLength(Exception):
    """Raised when the value entered is empty"""
    pass
class ValueAlphabetic(Exception):
    """Raised when the value is alphabetic"""
    pass

Then create a function that will hold all the try except logic to check for the errors

try:
        integervalue = input("Type something to test this out: ") # Creates a string here
        if len(integervalue) == 0:
            raise ValueLength
        elif integervalue.isalpha():
            raise ValueAlphabetic
        elif int(integervalue) != 55 and int(integervalue) < 10: # converting to integer to allow comparison
            raise ValueTooSmall
        elif int(integervalue) != 55 and int(integervalue) > 10: # converting to integer to allow comparison
            raise ValueError
        else:
            print("55 entered correctly")
    except ValueError:
        raise ValueError("Integervalue needs to be fifty five")
    except TypeError:
        raise TypeError("Number needs to be entered")
    except ValueTooSmall:
        raise ValueTooSmall("Number needs to be greater than 10")
    except ValueLength:
        raise ValueLength("Number needs to be entered, can't be empty")
    except ValueAlphabetic:
        raise ValueAlphabetic("You cannot enter an alphabetic value")
    finally:

Putting it all together

class ValueTooSmall(Exception):
    """Raised when the input value is too small"""
    pass
class ValueLength(Exception):
    """Raised when the value entered is empty"""
    pass
class ValueAlphabetic(Exception):
    """Raised when the value is alphabetic"""
    pass

def checkvalue():
    try:
        integervalue = input("Type something to test this out: ") # Creates a string here
        if len(integervalue) == 0:
            raise ValueLength
        elif integervalue.isalpha():
            raise ValueAlphabetic
        elif int(integervalue) != 55 and int(integervalue) < 10: # converting to integer to allow comparison
            raise ValueTooSmall
        elif int(integervalue) != 55 and int(integervalue) > 10: # converting to integer to allow comparison
            raise ValueError
        else:
            print("55 entered correctly")
    except ValueError:
        raise ValueError("Integervalue needs to be fifty five")
    except TypeError:
        raise TypeError("Number needs to be entered")
    except ValueTooSmall:
        raise ValueTooSmall("Number needs to be greater than 10")
    except ValueLength:
        raise ValueLength("Number needs to be entered, can't be empty")
    except ValueAlphabetic:
        raise ValueAlphabetic("You cannot enter an alphabetic value")
    finally:
        print("All checks completed")


checkvalue()

You will notice that the Classes we created are referenced within the code to call the exact exception we are looking to capture.

For this reason what are the benefits of approaching it this way:

  • You can capture exceptions tailored to the code you are writing.
  • Controls can easily be created and implemented so that specific errors with your data can be flagged straight away.
  • If you are going to be looking to catch the same exceptions in numerous places, this methodology will help to implement a better structure to handle those.
  • You can start to build a centralised repository of centralized exceptions and how to handle them.
  • All contributors to your project know how to raise an exception in python , you do not get multiple different v.

On our YouTube channel you can subscribe to find out more information about exceptions, and lots of tips and tricks for better Data Analytics.