• Register
1 vote
551 views

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

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:

https://github.com/uber/ludwig/issues/76

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”.

Solution:

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

Example:

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])

Output:

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])

Output:

Value at List Index 1 physics

Note:

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 1
3,890 points
0 votes

Solution:

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
print(string1)

 

Output

Hi This is STechies

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 10 views
1 vote
2 answers 24 views
24 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 LizzyM 6.1k points
3 votes
2 answers 174 views
174 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 Gavin 15.3k points
1 vote
1 answer 21 views
21 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 samhaz 5k points
0 votes
1 answer 459 views
459 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 jwilliam 3.9k points