• Register
1 vote

Problem :

 I'm a newbie with Python's arrays and I don't understand how I can avoid below error:
python typeerror: list indices must be integers or slices, not str
6 5 3
6,930 points

Please log in or register to answer this question.

3 Answers

0 votes

Solution :

Please Note array_length should be an integer and not a string:

array_length = len(array_dates)

Also note, your for loop should be constructed using range:

for i in range(array_length):  # Use `xrange` for python 2.

Third, i will increment automatically, so delete the bellow line:

i += 1

Further Readings:


9 7 4
38,600 points
0 votes

Every element in python has a unique position. These positions are defined by index and these indexes are always represented by using integers. You have declared a variable that has an index value of the list element. But if the element does not have integer value instead has a string value. Then the following error occurs;

“TypeError: list indices must be integer or slices, not str”.


To avoid this error, it is suggested that always assign an integer value to a variable.


To better understand this error lets look at an example:

# Declare a list

list1 = ['Physics','Chemistry', 'Biology']

# Declare an index variable

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.

Correct Example:

# Initialise a list

list1 = ['Physics','Chemistry', 'Biology']

# Declare an index variable

index = 1

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


Value at List Index 1 physics


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

11 5 2
3,890 points
0 votes


1st , array_length must be an integer and not a string

array_length = len(array_dates)

2nd , Your for loop must be constructed using range

for i in range(array_length):  # Use `xrange` for python 2.

3rd, i will increment automatically

i += 1


Follow the below 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



Hi This is STechies


10 6 4
31,120 points

Related questions

0 votes
1 answer 21 views
0 votes
1 answer 14 views
Problem: Hello! I am completely new to Python and trying to grasp concepts in Python. While I was learning lists and dictionaries, I wrote the following program: name = ["John", "Alice", "Catherine"] diction = {"name" : "Alice"} print(name[diction]) When I ... ; as a value and the program should print &ldquo;Alice&rdquo; on screen. I do not know what I have done wrong. Can anybody help?
asked Dec 18, 2020 Code Learner 5.3k points
1 vote
2 answers 45 views
Problem: I wrote a program: def only(): names = ['Laia', 'Poter', 'Harrold', 'Annabell', 'Preety', 'Whina', 'Cory', 'Phase'] put = input('Search with name:') binary_search(names, put) if position == -1: print("The name entered is not part ... in binary_search if names[middle] == entered: TypeError: list indices must be integers or slices, not float Why is this error happenning can anyone explain?
asked Apr 3, 2020 LizzyM 6.1k points
3 votes
2 answers 230 views
Problem: I am trying to learn python3 and tuple. It was going pretty smooth unless I tried to access an element from a tuple of my program. It throws back an error: typeerror: tuple indices must be integers or slices, not tuple What's wrong? Any explanation?
asked Mar 25, 2020 Gavin 15.3k points
1 vote
1 answer 28 views
Problem: I have a program where I used tuple and it is throwing e some eror message. The code I worte is : import random board = [] for x in range(0,5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) print_board(board) ... len(board)-1 or guess_col < 0 or guess_col > len(board[0])-1: TypeError: list indices must be integers not tuple How to solve this thing?
asked Apr 27, 2020 samhaz 5k points
0 votes
1 answer 671 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24, 2020 jwilliam 3.9k points
0 votes
1 answer 13 views
What is the difference between array and list in python?
asked Oct 10, 2020 sikandar 2.4k points
1 vote
1 answer 67 views
Problem : I am very new to Programming. Currently I am trying to learn the Python. I am trying to slice my list (myplateau) in the several list (L[i]) but because of some unknown reasons to me I am facing the below error message: File "C:\Users\Martin\Skycraper ... ]     L.append(myitem)     i += 1 I am very confused about above error. Please help me in fixing above mentioned python related error.
asked May 13, 2020 Martin K 6.6k points
1 vote
1 answer 299 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11, 2020 samhaz 5k points