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 15, 2020 in Python by akhtar
• 38,230 points
13,412 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 15, 2020 by MD
• 95,440 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, 2020 in Python by anonymous
• 65,910 points
2,047 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, 2020 in Python by Peddabudi srikanth
• 120 points
4,394 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 19, 2020 in Python by peyman
• 120 points

edited Oct 19, 2020 by Gitika 887 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,077 views
0 votes
1 answer
+5 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,790 points
3,500 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, 2020 in Python by MD
• 95,440 points
28,298 views
+1 vote
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, 2020 in Python by MD
• 95,440 points
9,388 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP