Given the list called my_list, what would the following slice notation give you:
my_list[-1:-5:-1]
Explanation
The syntax is
   list_name[start:end:step]
Since step is -1, we are counting backwards. Since start is -1, we start at the end of the list. Then we count backwards to (but not including) index -5. So, assuming the list contains four or more items, we'll get values for indexes -1, -2, -3, and -4.
In case list has less than 4 elements - code will return all available elements in reverse order.

@tusculum66 thanks for a great explanation! added to the question

2017 Dec 5, 3:33:48 PM

The syntax is list_name[start:end:step] Since step is -1, we count backwards. Since start is -1, we start at the end of the list. Then we count backwards to (but not including) index -5. So, assuming the list contains four or more items, we'll get values for indexes -1, -2, -3, and -4. That will reverse the order of the elements in the list. I hope that helps...

2017 Nov 28, 2:16:36 PM

Why?

2017 Nov 21, 9:18:02 PM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Python quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes