how to get INDEX count, or last number of Index
Terry Reedy
tjreedy at udel.edu
Wed May 23 22:37:25 EDT 2018
On 5/23/2018 8:46 PM, bartc wrote:
> On 24/05/2018 00:44, Terry Reedy wrote:
>> On 5/23/2018 5:56 PM, Rob Gaddi wrote:
>>> On 05/23/2018 02:51 PM, asa32sd23 at gmail.com wrote:
>>>> s = "kitti"
>>>>
>>>> 0,1,2,3,4
>>>> k,i,t,t,i
>>>>
>>>> how do i retrieve '4'. i know i can do a len(s)-1,
>>
>> Use -1, which is the same as len(s)-1 but faster.
>
> This illustrates one problem with having a example sequence of values
> being identical to the indices of those values.
>
> I would have used 10,20,30,40,50 so there could be no such mix-up.
>
> Because I assumed here that the OP wanted the index of the last value,
> the '4' they said they wanted, not the last value itself which would be
> 'i'.
>
> And actually, the subject line seems to confirm that.
>
> In that case, using a '-1' index won't work.
You snipped the code that shows that -1 does work to fetch the last
character.
>>> s = 'kitty'
>>> s[len(s)-1]
'y'
>>> s[-1]
'y'
--
Terry Jan Reedy
More information about the Python-list
mailing list