Click here to Skip to main content
15,916,042 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It shows throws
id = [int(data[-1]['emp_id'][6:])]
TypeError: list indices must be integers or slices, not str.

how to solve this ?

What I have tried:

id = [int(data[-1]['emp_id'][6:])]
emp_id = id[-1] + 1
id.append(str(emp_id))
Posted
Updated 13-Jun-22 4:47am

As the message clearly states, you cannot use a string type to address elements of a list. The index value 'emp_id' is not allowed. Maybe you meant to use that variable name without the quotes:
Python
id = [int(data[-1][emp_id][6:])]
 
Share this answer
 
Comments
CPallini 13-Jun-22 10:45am    
5.
Quote:
TypeError: list indices must be integers or slices, not str.

Python
id = [int(data[-1]['emp_id'][6:])]
//                 ^ here is the error

Python expect integer indices 0, 1, 2, 3 ...
I fear Python will chock on -1 and 6: too.
NumPy Array Indexing[^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 13-Jun-22 10:55am    
No, Python will be happy with -1, which is the last item in the list, and 6: which is the slice offset 6 to the end. See 3. An Informal Introduction to Python — Python 3.10.5 documentation[^].
Patrice T 13-Jun-22 11:06am    
Thanks for information, I am not Python user :)
Richard MacCutchan 13-Jun-22 11:24am    
Python has lots of features that are a bit strange until you get used to them.

Also, if you read it, you will see that NumPy uses the same indexing as Python.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900