list indices must be integers or slices, not str

Frank Millman frank at chagford.com
Wed Jul 20 04:32:07 EDT 2022


Hi all

C:\Users\E7280>python
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>>
 >>> x = list(range(10))
 >>>
 >>> '{x[1]}'.format(**vars())
'1'
 >>>
 >>> '{x[-1]}'.format(**vars())
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
 >>>

Can anyone explain this error? It seems that a negative index is deemed 
to be a string in this case.

Thanks

Frank Millman


More information about the Python-list mailing list