python - For x in string , how to check if x is last -


is there way know if x last element of string in following basic example of loop

for x in string_time:      

if want know whether x physically last element in string, can use enumerate():

for i,x in enumerate(string_time, start=1-len(string_time)):     if not i:         # last element     ... 

if, on other hand, want know whether x equal last element, can use == (as mentioned in comments):

for x in string_time:     if x == string_time[-1]:         # last element     ... 

just describe what's going on in first snippet: we're enumerating string starting @ 1-len(string), following:

>>> s = 'abc' >>> >>> list(enumerate(s, start=1-len(s))) [(-2, 'a'), (-1, 'b'), (0, 'c')] 

so last element enumerated 0, meaning can use not i check check if we're on last element.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -