Last updated: Apr 8, 2024
Reading time·9 min
The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index.
To solve the error, use the int()
class to convert the string to an integer,
e.g. my_list[int(my_str)]
.
Here is an example of how the error occurs.
my_str = '1' my_list = ['a', 'b', 'c'] # ⛔️ TypeError: list indices must be integers or slices, not str result = my_list[my_str]
The error is caused because we are using a string as a list index.
a_list = ['a', 'b', 'c', 'd', 'e'] # ✅ Indices must be integers print(a_list[0]) # 👉️ 'a' print(a_list[1]) # 👉️ 'b'
To solve the error, use an integer or a slice for list indices.
my_str = '1' my_list = ['a', 'b', 'c'] # ✅ Convert str to int result = my_list[int(my_str)] print(result) # 👉️ 'b'
We used the int() class to convert the string to an integer to be able to access the list at an index.
0
, and the last item has an index of -1
or len(a_list) - 1
.You also need to convert the indices to integers if you need to get a slice of a list.
start_index = '0' stop_index = '2' a_list = ['a', 'b', 'c', 'd', 'e'] new_list = a_list[int(start_index):int(stop_index)] print(new_list) # 👉️ ['a', 'b']
Notice that we used the int()
class to convert both values to integers.
The syntax for list slicing is
a_list[start:stop:step]
.
a_list = ['a', 'b', 'c', 'd', 'e'] print(a_list[0:2]) # 👉️ ['a', 'b'] print(a_list[1:3]) # 👉️ ['b', 'c']
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
If you need to iterate over a list with access to the index of the current
iteration, use enumerate()
.
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com
The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.
You can also use the range()
class to get access to the index.
my_list = ['bobby', 'hadz', 'com'] for index in range(len(my_list)): # 0 bobby # 1 hadz # 2 com print(index, my_list[index])
If you need to check if a value is in a list, use the in
operator.
a_list = ['a', 'b', 'c'] if 'a' in a_list: # 👇️ this runs print('The value is in the list') else: print('The value is not in the list') print('a' in a_list) # 👉️ True print('Z' in a_list) # 👉️ False
The in
operator returns True
if the value is in the list and false
otherwise.
Note that the input()
function always returns a string even if the user enters
an integer.
a_list = ['a', 'b', 'c', 'd'] index = input('Enter an index: ') print(index) # 👉️ '1' print(type(index)) # 👉️ string # ⛔️ TypeError: list indices must be integers or slices, not str print(a_list[index])
The input()
function converts the supplied value to a string and returns it.
To solve the error, use the int()
class to convert the value to an integer
before accessing the list.
a_list = ['a', 'b', 'c', 'd'] # ✅ Convert to integer index = int(input('Enter an index: ')) print(index) # 👉️ 1 print(type(index)) # 👉️ <class 'int'> print(a_list[index]) # 👉️ b
If you meant to declare a dictionary, make sure to use curly braces (not square brackets).
my_dict = {} my_dict['name'] = 'Bobby Hadz' my_dict['age'] = 30 print(my_dict) # 👉️ {'name': 'Bobby Hadz', 'age': 30}
If you are iterating over a list of dictionaries, make sure to access key-value pairs of the dictionary on each iteration.
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] for entry in my_list: print(f'Employee id: {entry["id"]}') print(f'Employee name: {entry["name"]}')
We used a for
loop to iterate over a list of dictionaries.
entry
variable stores a dictionary on each iteration, so you can access it at a specific key to get the corresponding value.If you need to access the index of the current iteration, use the enumerate()
function.
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] for index, dictionary in enumerate(my_list): print(index, dictionary) print(dictionary['id'], dictionary['name'])
You can use an index to access a specific dictionary in the list and then access a key in the dictionary.
my_list = [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] result = my_list[0]['name'] print(result) # 👉️ 'Alice'
We access the list item at index 0
and then access the name
key of the
dictionary.
Use the list.index()
method if you need to get the index of a value in a list.
a_list = ['a', 'b', 'c'] print(a_list.index('a')) # 👉️ 0 print(a_list.index('b')) # 👉️ 1
The list.index()
method returns the index of the first item whose value is
equal to the provided argument.
If you have a dictionary with a list value, access the specific key before accessing an index.
my_dict = { 'fruits': ['apple', 'banana', 'kiwi'] } result = my_dict['fruits'][0] print(result) # 👉️ 'apple'
If you need to iterate over a dictionary, use the dict.items()
method.
my_dict = {'name': 'Alice', 'age': 30} for key, value in my_dict.items(): print(key, value) # 👉️ name Alice, age 30
The dict.items method returns a new view of the dictionary's items ((key, value) pairs).
If you get the error when working with a JSON string, make sure to parse the JSON into a native Python object before accessing specific items.
import json my_json = json.dumps( [ {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ] ) print(type(my_json)) # 👉️ <class 'str'> # ✅ convert to native Python object my_list = json.loads(my_json) print(my_list[0]['name']) # 👉️ 'Alice' print(my_list[1]['name']) # 👉️ 'Bob' print(type(my_list)) # 👉️ <class 'list'>
The json.loads method parses a JSON string into a native Python object.
Conversely, the json.dumps method converts a Python object to a JSON formatted string.
If you aren't sure what type of object a variable stores, use the type()
class.
my_dict = {'name': 'Alice', 'age': 30} print(type(my_dict)) # 👉️ <class 'dict'> print(isinstance(my_dict, dict)) # 👉️ True my_list = ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: tuple indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a tuple at a specific index.
To solve the error, use the int()
class to convert the string to an
integer.
Here is an example of how the error occurs.
my_tuple = ('a', 'b', 'c') my_str = '1' # ⛔️ TypeError: tuple indices must be integers or slices, not str result = my_tuple[my_str]
The error is caused because we are using a string as a tuple index.
my_tuple = ('a', 'b', 'c', 'd') # ✅ indices must be integers print(my_tuple[0]) # 👉️ a print(my_tuple[1]) # 👉️ b print(my_tuple[1:3]) # 👉️ ('b', 'c')
To solve the error, convert the string to an integer using the int()
class.
my_tuple = ('a', 'b', 'c') my_str = '1' # ✅ convert str to int result = my_tuple[int(my_str)] print(result) # 👉️ 'b'
The code sample uses the int()
class to convert the string index to an integer
when accessing the tuple.
0
, and the last item has an index of `-1`
or len(my_tuple) - 1
.If you need to get a slice of a tuple, both indices must be integers.
my_tuple = ('a', 'b', 'c', 'd') start_index = '1' stop_index = '3' new_tuple = my_tuple[int(start_index):int(stop_index)] print(new_tuple) # 👉️ ('b', 'c')
The syntax for
tuple slicing
is my_tuple[start:stop:step]
.
my_tuple = ('a', 'b', 'c', 'd') print(my_tuple[0:2]) # 👉️ ('a', 'b') print(my_tuple[1:3]) # 👉️ ('b', 'c')
The start
index is inclusive and the stop
index is exclusive (up to, but not
including).
If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the tuple.
You can use a simple for loop to iterate over a tuple.
my_tuple = ('a', 'b', 'c', 'd') for item in my_tuple: print(item) # 👉️ a, b, c, d
If you need to get access to the index of the current iteration, use the
enumerate()
function.
my_tuple = ('a', 'b', 'c') for index, item in enumerate(my_tuple): # 0 a # 1 b # 2 c print(index, item)
If you need to check if an item is in a tuple, use the in
operator.
my_tuple = ('a', 'b', 'c') print('a' in my_tuple) # 👉️ True print('hello' in my_tuple) # 👉️ False
The in
operator returns True
if the item is in the tuple and False
otherwise.
If you meant to declare a dictionary, make sure to use curly braces (not parentheses).
my_dict = {} my_dict['name'] = 'Alice' my_dict['age'] = 30 print(my_dict) # 👉️ {'name': 'Alice', 'age': 30}
If you are iterating over a tuple of dictionaries, make sure to access key-value pairs of the dictionary on each iteration.
my_tuple = ( {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ) for entry in my_tuple: print(entry['id']) print(entry['name'])
We used a for
loop to iterate over a tuple of dictionaries.
entry
variable stores a dictionary on each iteration, so you can access it at a specific key to get the corresponding value.You can use an index to access a specific dictionary in the tuple and then access a key in the dictionary.
my_tuple = ( {'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Carl'}, ) result = my_tuple[0]['name'] print(result) # 👉️ 'Alice'
We access the tuple item at index 0
and then access the name
key of the
dictionary.
If you have a dictionary with a tuple value, access the specific key before accessing an index.
my_dict = { 'fruits': ('apple', 'banana', 'kiwi') } result = my_dict['fruits'][0] print(result) # 👉️ 'apple'
If you need to iterate over a dictionary, use the dict.items()
method.
my_dict = {'name': 'Alice', 'age': 30} for key, value in my_dict.items(): print(key, value) # 👉️ name Alice, age 30
The dict.items() method returns a new view of the dictionary's items ((key, value) pairs).
In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIf you aren't sure what type of object a variable stores, use the type()
class.
my_tuple = ('a', 'b', 'c') print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_list = ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: slice indices must be integers or None or have an
__index__ method" occurs when we use a non-integer value for slicing (e.g. a
float
).
To solve the error, make sure to use integers when slicing a list, string or any other sequence.
Here is an example of how the error occurs.
my_list = ['a', 'b', 'c', 'd'] start = 0.5 stop = 2.5 # ⛔️ TypeError: slice indices must be integers or None or have an __index__ method result = my_list[start:stop]
We used floats for the start and stop values when slicing the list.
start
, stop
and step
values.The syntax for list slicing is my_list[start:stop:step]
.
To solve the error, convert the floating-point values to integers.
my_list = ['a', 'b', 'c', 'd'] start = 0.5 stop = 2.5 result = my_list[int(start):int(stop)] print(result) # 👉️ ['a', 'b']
We passed the floating-point numbers to the int()
class to convert them to
integers.
start
is inclusive whereas the value for stop
is exclusive.my_list = ['a', 'b', 'c', 'd'] print(my_list[0:2]) # 👉️ ['a', 'b'] print(my_list[1:3]) # 👉️ ['b', 'c']
If the start
index is omitted, it is considered to be 0
, if the stop
index
is omitted, the slice goes to the end of the list.
The error often occurs when we use the division operator /
because the
division operator /
always produces a float value
print(10/2) # 👉️ 5.0
Division /
of integers yields a float, while
floor division //
of integers results
in an integer.
print(10//2) # 👉️ 5
floor()
function applied to the result.Once you use integers to slice the sequence, the error will be resolved.
If you aren't sure what type of object a variable stores, use the built-in
type()
class.
my_int = 10 print(type(my_int)) print(isinstance(my_int, int)) my_float = 3.14 print(type(my_float)) print(isinstance(my_float, float))
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.