5-Intro To Lists
5-Intro To Lists
When doing data science, you need a way to organize your data so you can work with it efficiently. Python has
many data structures available for holding your data, such as lists, sets, dictionaries, and tuples. In this tutorial, you
will learn how to work with Python lists.
Motivation
In the Petal to the Metal competition, your goal is to classify the species of a flower based only on its image. (This
is a common task in computer vision, and it is called image classification.) Towards this goal, say you organize the
names of the flower species in the data.
In [1]:
flowers = "pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,eng
lish marigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle"
print(type(flowers))
print(flowers)
<class 'str'>
pink primrose,hard-leaved pocket orchid,canterbury bells,sweet pea,english ma
rigold,tiger lily,moon orchid,bird of paradise,monkshood,globe thistle
Even better is to represent the same data in a Python list. To create a list, you need to use square brackets
( [ , ] ) and separate each item with a comma. Every item in the list is a Python string, so each is enclosed in
quotation marks.
In [2]:
flowers_list = ["pink primrose", "hard-leaved pocket orchid", "canterbury bells",
"sweet pea", "english marigold", "tiger lily", "moon orchid", "bird of paradise",
"monkshood", "globe thistle"]
print(type(flowers_list))
print(flowers_list)
<class 'list'>
['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pe
a', 'english marigold', 'tiger lily', 'moon orchid', 'bird of paradise', 'mon
kshood', 'globe thistle']
At first glance, it doesn't look too different, whether you represent the information in a Python string or list. But as
you will see, there are a lot of tasks that you can more easily do with a list. For instance, a list will make it easier to:
Lists
Length
We can count the number of entries in any list with len() , which is short for "length". You need only supply the
name of the list in the parentheses.
In [3]:
# The list has ten entries
print(len(flowers_list))
10
Indexing
We can refer to any item in the list according to its position in the list (first, second, third, etc). This is
called indexing.
In [4]:
print("First entry:", flowers_list[0])
print("Second entry:", flowers_list[1])
Side Note: You may have noticed that in the code cell above, we use a single print() to print multiple items
(both a Python string (like "First entry:" ) and a value from the list (like flowers_list[0] ). To print multiple
things in Python with a single command, we need only separate them with a comma.
Slicing
You can also pull a segment of a list (for instance, the first three entries or the last two entries). This is
called slicing. For instance:
As you can see above, when we slice a list, it returns a new, shortened list.
Removing items
Remove an item from a list with .remove() , and put the item you would like to remove in parentheses.
In [6]:
flowers_list.remove("globe thistle")
print(flowers_list)
Adding items
Add an item to a list with .append() , and put the item you would like to add in parentheses.
In [7]:
flowers_list.append("snapdragon")
print(flowers_list)
As an example, consider hardcover book sales in the first week of April 2000 in a retail store.
In [8]:
hardcover_sales = [139, 128, 172, 139, 191, 168, 170]
Here, hardcover_sales is a list of integers. Similar to when working with strings, you can still do things like get
the length, pull individual entries, and extend the list.
In [9]:
print("Length of the list:", len(hardcover_sales))
print("Entry at index 2:", hardcover_sales[2])
You can also get the minimum with min() and the maximum with max() .
In [10]:
print("Minimum:", min(hardcover_sales))
print("Maximum:", max(hardcover_sales))
Minimum: 128
Maximum: 191
We can also do similar calculations with slices of the list. In the next code cell, we take the sum from the first five
days ( sum(hardcover_sales[:5]) ), and then divide by five to get the average number of books sold in the first
five days.
In [12]:
print("Average books sold in first five days:", sum(hardcover_sales[:5])/5)
Your turn
Now it's your turn to practice creating and modifying lists.
Have questions or comments? Visit the course discussion forum to chat with other learners.