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

0 votes

Hi Guys,

I am new to python. I am trying to run one python code. But I am getting the below error.

list1 = ['Apple','Banana', 'Oranges']
index = '1'
print('Value at List Index 1', list1[index])

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
Oct 14 in Python by akhtar
• 33,370 points
902 views

1 answer to this question.

0 votes

Hi@akhtar,

As we know that the index of every list is an integer, and in your example, you have declared the index value as a string. So you need to change the code as shown below.

list1 = ['Apple','Banana', 'Oranges']
index = 1
print('Value at List Index 1', list1[index])
answered Oct 14 by MD
• 79,190 points

Related Questions In Python

+1 vote
1 answer

Can anyone help me with this error: TypeError: list indices must be integers or slices, not str

Hi, @Varshap  It’s a TypeError, which tells us ...READ MORE

answered Nov 5 in Python by anonymous
• 49,580 points
78 views
0 votes
3 answers
0 votes
0 answers

TypeError: tuple indices must be integers or slices, not tuple

Traceback (most recent call last):   File "main.py", line ...READ MORE

Aug 25 in Python by Peddabudi srikanth
• 120 points
797 views
0 votes
0 answers

TypeError: tuple indices must be integers or slices, not tuple

Im a begginer and im trying to ...READ MORE

Oct 18 in Python by peyman
• 120 points

edited Oct 19 by Gitika 72 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,561 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,181 views
0 votes
1 answer

ValueError("arrays must all be same length") ValueError: arrays must all be same length

Hi,  You need to specify index for your ...READ MORE

answered Jun 25 in Python by MD
• 79,190 points
5,808 views
0 votes
1 answer

ImportError: DLL load failed: The specified module could not be found.

Hi@akhtar, You may get this error if you ...READ MORE

answered Jun 22 in Python by MD
• 79,190 points
3,019 views