# How to resolve TypeError: list indices must be integers or slices, not tuple in Python

If you are having trouble finding solutions to the TypeError: list indices must be integers or slices, not tuple error, then the following article is for you. In this article, I will outline the cause of the error and two ways to fix it. I hope you will read it all.

## What causes the TypeError: list indices must be integers or slices, not tuple?

The list is one of the most used sequential data types in Python. The feature of sequential structure types is that they consist of many elements, and each element is numbered for easy access.

Initialize a list with square brackets:

List = [ value1, value2, value3, ….]

The list index starts at 0 and ends at the number of elements – 1.

TypeError: Python throws this error when you perform an unsupported operation on an object with an invalid data type.

The TypeError: list indices must be integers or slices, not tuple happens because the list index is a tuple and not an integer or slice.

Example:

listInfor = ['John', '18 years old', 'Dev']
ValueTuple = (0, 1)

# Pass a tuple into square brackets when accessing the list index
print(listInfor[ValueTuple])

Output:

Traceback (most recent call last):
File "./prog.py", line 5, in <module>
TypeError: list indices must be integers or slices, not tuple

## How to solve this error?

### Access the list index

As stated above, the list elements are numbered to facilitate information retrieval. The simple workaround is that the index of the list must be numbers within the range of valid indexes.

Example:

• Initialize a list of elements.
• Perform index access that retrieves the elements in the list.
listInfor = ['John', '18 years old', 'Dev']

# Access the list index
print('The first element of the list:', listInfor[0])
print('The second element of the list:', listInfor[1])
print('The third element of the list:', listInfor[2])

Output:

The first element of the list: John
The second element of the list: 18 years old
The third element of the list: Dev

For nested lists, you need to access the index multiple times if you want to access each list element.

Example:

nestedList = [['visit', 'leanrnshareit'], ['website']]

print(nestedList[0])

# Access each 1st sublist element
print(nestedList[0][0])
print(nestedList[0][1])

# If the index is 1, perform access to the 2nd sublist
print(nestedList[1])

# Access each 2nd sublist element
print(nestedList[1][0])

Output:

['visit', 'leanrnshareit']
visit
leanrnshareit
['website']
website

### Use the slicing operation.

The slicing operator is also similar to a list index. Still, the difference is that it retrieves a sublist from the original list, and the index takes only the individual elements of the list.

Syntax:

[start:end]

Parameters:

• start: is the starting position to get the value. If left blank, the default is to get from the beginning of the tuple.
• end: end position. If left blank, all values ​​in the tuple will be taken.

Example:

• Initialize a list of elements.
• Use the slicing operation to retrieve sublists from the original list.
• You can customize the parameters of the syntax.
myList = ['John', '18 years old', 'Dev']

# Use the slicing operation
print('Get the first element to the end:', myList[0:])
print('Get the second element to the end:', myList[1:])
print('Get the third element to the end:', myList[2:])

Output:

Get the first element to the end: ['John', '18 years old', 'Dev']
Get the second element to the end: ['18 years old', 'Dev']
Get the third element to the end: ['Dev']

## Summary

Through the article, have you been able to fix the TypeError: list indices must be integers or slices, not tuple? Remember that lists are sequential structures, so accessing the list index must be done by numbers or slicing operations with the same syntax as above. Thank you for taking the time to read the article.