• Register
1 vote
700 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

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:

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 2
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 39 views
0 votes
1 answer 119 views
119 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 9.2k points
0 votes
1 answer 2 views
2 views
Problem: Any ideas on what I need to fix this?list indices must be integers or slices, not float
asked Apr 8 ashik 12.9k points
1 vote
2 answers 169 views
169 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
0 votes
1 answer 17 views
17 views
Problem: I am trying to pull data from a database and assign them to different lists. This specific error is giving me a lot of trouble "TypeError: tuple indices must be integers, not str" I tried converting it to float and etc but with no success. Any help would be appreciated! Thanks people!
asked Mar 16 ummesalma 25.2k points
0 votes
1 answer 18 views
18 views
Problem: I've got two lists of coordinates, they look like this: list_kp2_ok: [[1185.60009765625, 933.6000366210938], [1310.4000244140625, 828.0000610351562], [1067.0, 979.0], [1310.0, 828.0], [1423.2000732421875, 814.800048828125], [1306.0, 828.0], [3634.0, 605.0 ... 828.6107788085938]] Now I need to check if there are any same coordinates on both lists and create a new list of them. So I wrote:
asked Feb 17 Mashhoodch 13k points
3 votes
2 answers 420 views
420 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
0 votes
1 answer 10 views
10 views
typeerror: tuple indices must be integers or slices, not tuple
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 23 views
23 views
Problem: I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything ... be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6. Thanks
asked Jan 27 Mashhoodch 13k points
0 votes
1 answer 16 views
16 views
Problem: I have been banging my head against this for two days now. I am new to python and programming so the other examples of this type of error have not helped me to much. I am reading through the documentation for lists and tuples, but haven't found anything ... would be much appreciated. Not looking for the answer necessarily, just more resources on where to look. I am using Python 2.7.6.
asked Jan 26 Mashhoodch 13k points