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, 2020 in Python by akhtar
• 38,180 points
9,205 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, 2020 by MD
• 95,220 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
1,136 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
2,614 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, 2020 in Python by peyman
• 120 points

edited Oct 19, 2020 by Gitika 289 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,174 views
0 votes
0 answers
+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,810 points
1,635 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,220 points
12,777 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,220 points
6,292 views