Say I have a listl = ['h','e','l','l','o']
I want to be able to take the last two items and use them, but I don't know how long the list
is(It's user input). So can't do l[3:]
because what if the list is seven(or any number) items long instead of five?
How can I count from the back of the list?
print l[-2:]
Negative numbers count from the end.
>>> l = ['h','e','l','l','o']
>>> print l[-2:]
['l', 'o']
>>> print l[-5:]
['h', 'e', 'l', 'l', 'o']
>>> print l[-6:]
['h', 'e', 'l', 'l', 'o']
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With