Online Tutorials & Training Materials | STechies.com
Register Login

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

27 Nov 2019 8:07 am || 0

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

Each element in a list in Python has a unique position. This position is defined by an index. These indexes are always defined using integers. You might declare a variable that has the index value of a list element. But if this variable does not have an integer value and instead has a string value, you will face an error called “TypeError: list indices must be integers or slices, not str”. The way to avoid encountering this error is to always assign an integer value to the variable.

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

To better understand this error, let's take a very simple example

Example:

# Declare a list
list1 = ['Apple','Banana', 'Oranges']

# Declare a index variable
index = '1'

print('Value at List Index 1', list1[index])

Output:

Traceback (most recent call last):
  File "list-error.py", line 5, in <module>
    print('Value at List Index 1', list1[index])
TypeError: list indices must be integers or slices, not str

In the above example, we have declared a list with the name “list1” and an index variable to access the value of list with the name ‘index’ and given value “1” as a string, not an integer.

index = '1' # Declare index variable as string

Correct

index = 1 # Declare index variable as integer

As we know that the index of every list is an integer, and in the above example, we have declared index value as a string.

Correct Example: 

# Inilise a list
list1 = ['Apple','Banana', 'Oranges']

# Inilise a index variable
index = 1

print('Value at List Index 1', list1[index])

Output:

Value at List Index 1 Banana

Example 2

# Inilised a list 
list1 = ['Hi', 'This', 'is','STechies']

# Inilised variable i as 0
i = 0

# Inilised empty string
string1 = ''

# Run While loop to list length
while i < len(list1):
    # Joint each value of list to final string
    string1 = string1 + ' ' + list1[string1]
    i += 1

# Print final Output
print(string1)

Output:

Traceback (most recent call last):
  File "list-error.py", line 13, in <module>
    string1 = string1 + ' ' + list1[string1]
TypeError: list indices must be integers or slices, not str

In the above example, we are accessing the value of list “list1” with variable “string1” which is a string variable.

Correct example:

# Inilised a list 
list1 = ['Hi', 'This', 'is','STechies']

# Inilised variable i as 0
i = 0

# Inilised empty string
string1 = ''

# Run While loop to list length
while i < len(list1):
    # Joint each value of list to final string
    string1 = string1 + ' ' + list1[i]
    i += 1

# Print final Output
print(string1)

Output:

Hi This is STechies

Conclusion

If you are using a variable for specifying the index of a list element, make sure it has an integer value. Otherwise, you can directly access an element using the indices.