In this module, we introduce Python’s list, the most commonly used data structure that is built into the language itself. Just like any list of stuff you encounter in everyday life, a list in Python is just a bunch of items arranged in some order. You can add items to a list and also remove items from it. What can the items be? Anything!
Why are lists so useful? Let’s recall what computers do so well - repetitive tasks. So a list basically contains a potentially enormous amount of little tasks (or data), and the computer simply goes through the items one after the next, very very quickly. The possibilities are endless!
So far, we’ve covered 3 main data types in Python: strings, integers, and booleans. Next, we’ll cover lists in Python. At it’s most basic level, a Python list is meant to keep a collection of other values in order. Here’s an example:
desserts = ['cupcake', 'ice cream', 'cookie']
In the above code, the variable desserts has the value of a Python list. While there are other data types contained in the list (strings, in this case), it’s important to recognize that the value of the variable desserts as a whole is a list.
There are a couple of important things to note about the way we can define lists in Python.
It’s possible to get the length of the list using the same built-in len function that we’ve used to get the length of a string:
>>> desserts = ['cupcake', 'ice cream', 'cookie'] >>> print len(desserts) 3
We can also store that value of 3 in a variable, like so:
>>> desserts = ['cupcake', 'ice cream', 'cookie'] >>> desserts_length = len(desserts) >>> print dessers_length
It’s possible make a list with no items.
desserts = 
What’s the length of an empty list?
An empty list’s length is zero
>>> desserts =  >>> print len(desserts) 0
It’s possible add a items to our empty desserts list using the method called append.
desserts =  desserts.append('cupcake')
What does our desserts list look like now? How could we check?
Let’s print the desserts list
>>> print desserts ['cupcake']
So, a one-item list is great, but we’d like to add a couple more items, like so:
desserts.append('ice cream') desserts.append('cookie')
What do you think the list looks like now?
The list has 3 separate strings in it. As each item was added, or more specifically appended, to the list, it was put at the end of the list.
We can ask for a particular item in a list, or index the list, using the following syntax:
>>> desserts 'cupcake' >>> desserts 'ice cream' >>> desserts 'cookie'
The place of an item in a list, with regards to the order of the
list, is called the item’s index. In the
desserts list, the string
has the index of 0, the string
has the index of 1, and the string
has the index of 2.
Interestingly, the first item in the list does not have the index of 1. Python list indexing starts at zero, rather than one. This can be a little tricky at first, so let’s do some practice.
Given the following list:
languages = ['Portuguese', 'English', 'Spanish', 'Russian', 'Mandarin']
How would you do the following?
Try it out!
Try this out in a repl session console by copying the languages list, and attempting to index the list.
The third item in languages has the index of 2.
The first item in languages has the index of 0.
The fifth item in languages has the index of 4.
If you try to access an item at an index that does not exist in a list, you get an IndexError. For example, given the following two-item list, there is item at index 0 and another item at index 1.
colors = ['purple', 'pink']
We can safely ask for both the item at index 0 and the item at index 1:
>>> print colors 'purple' >>> print colors 'pink'
However, if we try to get the item at index 2, there’s an error.
>>> print colors Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
As we’ll see in the control flow module, this is a fairly common error to get. However, its also a very easy error to fix. Getting an IndexError simply means you’re trying to get something that isn’t there.
Just like strings, lists have a variety of methods, or programmatic behaviors or capabilities. Methods are always notated like this:
The append method is perhaps the most common and important method for lists in Python.
Let’s learn about 2 more methods.
There are several ways to remove items from a list. In order to remove the last item in a list, you can use the method pop.
Here is an example:
>>> desserts = ['cupcake', 'ice cream', 'cookie'] >>> desserts.pop() 'cookie' >>> print desserts ['cupcake', 'ice cream']
After pop ran,
Try this out for yourself in the Python repl console.
One of the wonderful thing about lists in Python is that they
keep track of order. Once the list is defined, the order of
the items is maintained unless purposefully modified. So, the
desserts list will always have
second, and so on.
Let’s learn how to change the order! It’s conceivable that we would want a list of strings in alphabetical order, rather than the original order:
>>> desserts = ['cupcake', 'ice cream', 'cookie'] >>> desserts.sort() >>> print desserts ['cookie', 'cupcake', 'ice cream']
The list is now in alphabetical order. Nice!
Up until now, we’ve been storing strings in our lists. It’s also possible to make lists of integers and booleans as well. Here are a couple of examples:
>>> my_numbers = [ 2, 4, 6, 8, 10 ] >>> some_booleans = [ True, True, False, True, True ]
It’s the first item!
>>> print my_numbers 2 >>> type(my_numbers) <type 'int'>
It’s the third item!
>>> print some_booleans False >>> type(some_booleans) <type 'bool'>
Additionally, you can store many different data types in the same list. Here’s an example:
>>> stuff = ['cupcake', True, 47]
It’s the second item!
>>> print stuff True >>> type(stuff) <type 'bool'>
While strings and lists are different in many ways, they also share some of the same characteristics. Just like a list, it’s also possible to index a string.
For example, we let’s say we have a variable called
my_name and it’s value is the string
>>> my_name = "Balloonicorn" >>> something = my_name
What will happen when the following code runs?:
>>> print something
The first letter of
Using square brackets to index a string results in obtaining one of its characters. This is pretty nifty!
>>> my_name = "Balloonicorn" >>> something = my_name >>> print something B
>>> something = my_name
What will happen when the following code runs?:
>>> my_pet = "Arfy" >>> print my_pet
The third letter of
Since strings are also zero-indexed, asking for the letter at index 2 gives us the letter f.
>>> my_pet = "Arfy" >>> print my_pet f
At index 0 is the letter A, at index 1 is the letter r, and at index 2 is the letter f.
Now that we’ve learned lists in Python, it’s time to learn about a very common built-in function in Python that produces a list.
First, lets review other built-in functions we know. Which built-in functions have we learned about?
type and len
We’ve learned about 2 built-in functions so far. A built-in function is a function that can be called in any Python code, without having to define it or import it from somewhere else.
As a review, here’s how we’ve used type and len:
>>> my_message = "Hello world" >>> type(my_message) <type 'str'> >>> len(my_message) 11
type shows the data type for whatever is passed in to the type function. In this case, we passed a variable, my_message, whose value is a string.
len shows the length of whatever is passed in. In this case, we passed in a string which has 11 characters (including the space).
A third built-in function is called range. You must pass it in an integer as an argument, and it will return a list of integers.
>>> range(3) [0, 1, 2]
In this first example, we passed in 3 to the range function. So, range returned a list of 3 numbers. The numbers begin at 0, just like indices in lists and strings.
Here are a couple more examples:
>>> my_nums = range(4) >>> print my_nums [0, 1, 2, 3]
>>> more_nums = range(18) >>> print more_nums [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
>>> more_nums = range(5) >>> print more_nums [0, 1, 2, 3, 4]
Login to your account and start a new repl here. Title it module_4_practice.py.
Complete the practice problems below on Repl.it.
Use the range built-in function
The built-in function called range is very good at making lists of numbers. Use it!
The third item has the index of 2
Since the first item in your odd_nums list has the index of 0, the second has the index of 1, and the third has the index of 2.
The fifth item has the index of 4
Since the first item in your odd_nums list has the index of 0, the second has the index of 1, and so on, the fifth item has the index of 4.
>>> word1 = "delicious"
>>> word2 = "yummy"
>>> some_numbers = [4, 2, 1, 6, 8]
As a programmer, debugging is a fact of life. There are times you write code that Python doesn’t understand. In these cases, Python will display an error message. The more familiar you are with Python’s many error messages, the faster you’ll be at debugging code. But there’s good news: Python’s error messages are incredibly descriptive and helpful in figuring out what the problem is.
In the following problems, you’ll find code that is invalid or not allowed in some way. Read the code, and see if you can predict what is wrong. When you’re ready, hover over the solution area to reveal the error message that Python shows, along with an explanation of what is going wrong.
1) What’s wrong with this code?
>>> pizza_toppings = ['pepperoni', 'olives', 'mushrooms'] >>> print pizza_toppings
IndexError since there is no item at index 3
In order for a list to have something at the index of 3, there would need to be 4 items .
So, this code throws in IndexError.
>>> pizza_toppings = ['pepperoni', 'olives', 'mushrooms'] >>> print pizza_toppings Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
>>> desserts =  >>> desserts.add('cupcake')
Must use append to add an item to end of list
The list method to use in order to add something to the end of a list is called append. Lists don’t have a method called add.
So, this code throws in AttributeError.
>>> desserts =  >>> desserts.add('cupcake') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'add'
>>> desserts =  >>> print desserts
IndexError since there is no item at index 0
In order for a list to have something at the index of 0, there would need to be at least one item. An empty list has nothing at index 0.
So, this code throws in IndexError.
>>> desserts =  >>> print desserts Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
Create a new repl session called module_4_printing.py.
Ask the user as series of questions using raw_input, capturing their input into appropriately-named variables. The questions should be
Add each piece of user input to an empty list that you create. Please make sure to give the list an appropriate name.
Sort the list of places.
Then, tell the user where they said they’d like to go. Show each place on a separate line.
Copy the following list into your file:
>>> fun_words = ["elephant", "balloon", "macchiato", "angostura"]
Then, make a two empty lists– one called first_letters and one called third letters.
Using list_indexing along with string_indexing, make a variable for the first letter of each word in the fun_words list.
Need a hint?
If you index the fun_words list to get out a word, and then index the word to get out a letter, you’re halfway there.
first_word = fun_words first_letter_first_word = first_word
All that’s left is to put that letter into your first words list.
Copy the following 3 lists into your file:
>>> websites = ["facebook", "twitter", "buzzfeed"] >>> fruits = ["apple", "banana", "mango", "berry"] >>> names = ["Bob", "Alice", "Henry", "Rick", "Carl"]
Find the length of each of these lists, and store this value in three separate variables.
Make an empty list called lengths.
Add each length to the lengths lists.
Make a list of 25 numbers. Do not hard-code the numbers– that is, don’t type out 25 numbers yourself. Review the lesson if you’re not sure how to make a list of numbers using Python.
Once you have your list of 25 numbers, print out the first, fifth, tenth, and fifteenth item in your list.