August 2019
Beginner
482 pages
12h 56m
English
How do we retrieve one element from a list? How do we retrieve the last element of the list without computing its length explicitly?
To retrieve any element from a list, we can pass its index (order, starting with zero) in square brackets: mylist[0] will get the first element. Similarly, negative indices will return elements in reverse order—mylist[-1] will get the last element, no matter how many of them are stored.
How do we get all the elements of a list – except the first one and the last one – in reverse order?
For that, we can use slicing. In a slice, the first number represents the start, the second number represents the end, and the third one represents the step. A negative number will lead to the reverse order. Since we're ...