Lists and Indexing
A list defines a sequence of ordered objects (integers, floats, strings, and so on). They can be defined with comma-separated values between square…
Lists
A list defines a sequence of ordered objects (integers, floats, strings, and so on). They can be defined with comma-separated values between square brackets.Indexing
Indexing means the fetching of a single value from the list. This is a 0-based indexing scheme: the first item sits at index 0, the second at index 1, and so on.| index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| value | 2 | 3 | 5 | 7 | 11 |
| negative index | -5 | -4 | -3 | -2 | -1 |
Negative indices
Negative indices count from the end of the list. The last item is at index -1, the second to last at -2, and so on. They are a shortcut, saving you from writing L[len(L) - 1] just to reach the end.Modifying a list
Indexing can be used to SET elements as well as access them. An index on the left of an equals sign replaces the item in that slot.