Reputation: 21
So say that I have list a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. But say that depending on the rest of the program, I can't be sure how long a will be, but I want to display it using .format. How could I reference the last four elements? (without reversing the list and referencing the first four.)
Upvotes: 0
Views: 59
Reputation: 11477
I think this is what you want, use [-4:]
to get the last four elements, then unpack them:
>>> a=range(10)
>>> "{} {} {} {}".format(*a[-4:])
'6 7 8 9'
Hope this helps.
Upvotes: 1