TypeError: list indices must be integers or slices (Fix)

Fix the Python error TypeError: list indices must be integers or slices, not .... This error happens when you try to access a list with the wrong kind of value inside square brackets.

This page explains:

  • what the error means
  • why it happens
  • the most common ways to fix it
  • how to debug it quickly

Quick fix

numbers = [10, 20, 30]
index = 1
print(numbers[index])

# If the index comes from input, convert it first:
user_index = int(input("Enter index: "))
print(numbers[user_index])

List positions must be integers like 0 or 1, or slices like [1:3]. Strings, floats, and other lists cannot be used as list indices.

What this error means

A list index tells Python which position to access.

Valid list indexing looks like this:

  • my_list[0]
  • my_list[1]
  • my_list[-1]

Valid list slicing looks like this:

  • my_list[1:3]
  • my_list[:2]

The error appears when the value inside [] is the wrong type.

For example, this works:

items = ["a", "b", "c"]
print(items[0])

Output:

a

But this does not:

items = ["a", "b", "c"]
print(items["0"])

Python raises:

TypeError: list indices must be integers or slices, not str

If you need a refresher on how lists work, see Python lists explained for beginners.

Common example that causes the error

One of the most common causes is using a string instead of an integer index.

items = ["apple", "banana", "orange"]
print(items["0"])

This fails because "0" is a string, not the integer 0.

A very common reason for this is input(). Even when the user types a number, input() returns a string.

items = ["apple", "banana", "orange"]

index = input("Enter an index: ")
print(items[index])

If the user enters 1, Python still sees it as "1".

Fix 1: convert string input to an integer

If the value should be a list position, convert it with int().

items = ["apple", "banana", "orange"]

index = int(input("Enter an index: "))
print(items[index])

If the user enters 1, the output is:

banana

Use this fix only when the value really should be a numeric position.

If you want more help with this, read how to convert user input to numbers in Python.

Fix 2: loop over list items correctly

Another common mistake is treating each list value as if it were an index.

Wrong:

my_list = [10, 20, 30]

for i in my_list:
    print(my_list[i])

Why this fails:

  • i takes the values 10, 20, and 30
  • those are list items, not valid positions for this list

If you want the values, use them directly:

my_list = [10, 20, 30]

for i in my_list:
    print(i)

If you need positions, use range(len(...)):

my_list = [10, 20, 30]

for index in range(len(my_list)):
    print(index, my_list[index])

A better beginner-friendly option is enumerate():

my_list = [10, 20, 30]

for index, value in enumerate(my_list):
    print(index, value)

Output:

0 10
1 20
2 30

Fix 3: use dictionary access if your data uses keys

Lists use numeric positions.

Dictionaries use keys, which are often strings.

This works with a dictionary:

person = {"name": "Sam", "age": 25}
print(person["name"])

But this does not work if person is actually a list:

person = ["Sam", 25]
print(person["name"])

If your data uses names like "age", "title", or "name", a dictionary may be the right structure.

If you are seeing a missing-key problem instead, see KeyError in Python: causes and fixes.

Fix 4: check for accidental tuple or list indexing mistakes

Nested data can be confusing at first. Make sure you know what each level contains.

This can work:

data = [{"name": "Alice"}, {"name": "Bob"}]
print(data[0]["name"])

Why it works:

  • data is a list
  • data[0] is a dictionary
  • "name" is a valid dictionary key

But this fails:

data = [{"name": "Alice"}, {"name": "Bob"}]
print(data["name"])

This fails because data is a list, and lists cannot be indexed with the string "name".

When working with nested data, check each step carefully.

How to debug this error

When you see this error, inspect both:

  • the value used as the index
  • the object you are indexing

Useful debug lines:

print(index)
print(type(index))
print(my_list)
print(type(my_list))
print(len(my_list))

Example:

my_list = ["a", "b", "c"]
index = "1"

print(index)
print(type(index))
print(my_list)
print(type(my_list))
print(len(my_list))

print(my_list[index])

Output before the error:

1
<class 'str'>
['a', 'b', 'c']
<class 'list'>
3

This shows the problem clearly:

  • my_list is a list
  • index is a string
  • a string cannot be used as a list index

If you want to understand type() better, see Python type() explained.

What to remember

  • Lists need integer positions or slices
  • Strings from input() usually need conversion before being used as indices
  • Floats like 1.0 are not valid list indices
  • Use dictionaries when you need named keys instead of numeric positions

Common mistakes

Here are the most common causes of this error:

  • Using input() directly as a list index without int()
  • Using a string like '0' instead of the integer 0
  • Looping through list values and then using those values as indices
  • Confusing lists with dictionaries
  • Using a float such as 1.0 as an index
  • Indexing the wrong level of nested data

FAQ

Why does input() cause this error?

Because input() returns a string, and list indices must be integers or slices.

Can I use a float as a list index?

No. Even 1.0 is not valid. Convert it to an integer first if appropriate.

What is a slice in a list?

A slice selects a range of items, such as my_list[1:4].

When should I use a dictionary instead of a list?

Use a dictionary when you want to access values by names or keys like "name" or "age" instead of numeric positions.

See also