We have previously seen how to (1) construct lists (2) add elements to a list and (3) select elements from a list. We’ll to continue to develop our abilities to manipulate lists because they are central to how we manipulate data.
Python, as we’ve mentioned is a zero-based indexing language. That means if we want to select the first element of the list, we write my_list[0]
and if we wanted to select the second of the list my_list[1]
. Python also allows us, though, to select elements of a list by referencing their position relative to the end of the list. For example, we can select the last element by my_list[-1]
, and the second to last element by my_list[-2]
and so forth.
When working with lists, we’ll often have the need to select multiple elements from it. Formally, this idea is known as slicing. For example, we may have a really long list, say with a 1000 elements. To get a look at the first five elements, we can write my_list[0:5]
. The number to the left of the colon indicates the position of the element that we want to begin the slice at. The number to the right of the colon corresponds to the position in the list that we would like to slice up to, but not include (this might be slightly unintuitive at first). For example if we have my_list = ['car', 'dog', 'boat', 'scooter']
, writing my_list[1:3]
is equal to ['dog', 'boat']
. The 3
indicates that we are to slice up to, but not include the element in the 3rd position of the list. With slicing, Python has default values. We can omit the number to the left of the colon as in my_list[:2]
. Python reads this as my_list[0:2]
. And likewise, we can omit the number to the right of the colon. Python evaluates my_list[1:]
the same as my_list[1:len(my_list)]
.
<aside>
Check Your Understanding:
Create the same slice of the list using only positive integers: my_list[-3:]
</aside>
In Python, we can also work with lists of lists. That is a list where each element is a list. As an example, let’s say that your friend gives you the following list:
companies = ['Apple', 'Microsoft', 'Google', 'Amazon', 'Facebook', 'Tesla', 'IBM', 'Netflix', 'Samsung', 'Intel']
They tell you that now that they’re taking Business Analytics and are proficient in Python (or at least that’s what they wrote on their updated resume!), they are considering applying to internship positions at companies[1:2]
. Being the good friend that you are, you wonder if applying to only those companies is a great strategy. What if they don’t know anyone at the company so no one actually reads their application? What if they interview poorly one day and so their chances of receiving an internship drop by 50%? Over lunch one day, you and your friend agree that they should rank the companies in terms of which ones to prioritize during the application process, which ones to only apply to if they have extra time, and which ones to completely ignore.
Later that night, your friend sends you the following Python list via email asking for your thoughts.
current_thoughts = [companies[:3], [companies[4], companies[7]], companies[8:]]
They’ve sent you back a list of lists, but it’s hard to understand which inner list corresponds to the companies that they’re thinking of prioritizing and which ones are the ones that they’re going to completely ignore. Wouldn’t it be great to have labels associated with each inner list?
<aside>
Check Your Understanding:
True/False: companies[0] == companies[:1]
</aside>