Navigating collections
Let us have a variable with a list of Star Wars characters:
The star_wars variable stores a list of elements that are strings. In this lesson we will learn how to operate on lists in Python.
Negative indices
What happens if we give a negative index?
See example of a negative index
The program will display "Lando".
Index -1 simply means "take the first element from the end". Similarly, index -2 will return Jabba, -3 - Emperor, -4 - C3PO, and so on. Index -100 will throw an error, because the list doesn't have so many elements.
Extract from a list
We can download a fragment of a list by entering two indices indicating the start and end of the desired fragment. Separate them by a colon, just like you see below:
See example of a fragment of the list
The result will be:
Something's wrong here, isn't it? Since the initial and final indexes are given in brackets, it should display characters from Han to Chewie, inclusive!
Well, no. In Python the final index is the one that is no longer in the range. Simply put, add 1 to the index of the last element you want to extract. So star_wars[2:5] will extract the elements of the star_wars list with indexes 2, 3, 4, but not the one with index 5!
Empty indexes
If you omit the list index ...
See live example
... the following will happen:
- in the ch1 variable, there will be star_wars list elements displayed starting from the element with index 4 to the last element,
- the ch2 variable will contain star_wars list elements starting from the element with index 0 to the element with index 5 (remember that the end index does not fall into the range)
You can also use negative indexes in the ranges.
See live example
Result:
That is, from the fourth element from the end to the last element.
See example of negative indexes
Result:
Note that the fifth element from the end is R2D2, but this element is out of range.
Jumping
Python is designed so nicely that it even allows you to extract from the list not only a few , but for example every second, every third element - even counting from the end! You only need to add another parameter in square brackets (step), also separated with a colon.
See example of element extraction
Brilliant, isn't it?
But that's not all!
Let's go back to the beginning of the previous lesson, where we called the list a collection. A collection in computer science is an object that can store any number of other objects. So, in Python, a list is a collection. What else is a collection?
Strings
Yes! In Python a string is also a collection of single characters! And just as we can navigate a list, extract small fragments from it, reverse it - the same can be done with strings.
See example of text operations
Task 1
Analyze the code. Here you will find a list of all The Beatles' albums, sorted from the oldest (index 0) to the newest. Display the band's first and last album.
Do the task
See the solution
Task 2
Display the first five albums of the band on the screen.
Do the task
See the solution
Task 3
Display on the screen all albums from third to seventh inclusive.
Do the task
See the solution
Task 4
Now, display every second album, from the first to the last.
Do the task
See the solution
Task 5
Finally, display all the albums from the oldest to the latest (use reversing of the array).
Do the task
See the solution
Next chapter: Loops »
</div>
</div>