Friday, December 25, 2009

How to get the last item in a list

Code
items = ["hello","world"]                   #1
lastItem = items[-1]                        #2


How this works - line by line
items = ["hello","world"]                   #1

I'm setting a list called items for demonstration purposes.
The contents of this list are two strings - "hello" and "world"

lastItem = items[-1]                        #2

[-1] of the items list selects the last item in the list. 

No comments:

Post a Comment