Remember lists? They look like this:
books = ['Gender Trouble', 'Cruising Utopia', 'Living a Feminist Life']
For now, let’s just create a list and print it out. In a text editor, our script will look like this:
books = ['Gender Trouble', 'Cruising Utopia', 'Living a Feminist Life'] print(books)
Save this to a new file called
loop.py and run it with
python loop.py. You should see the list printed out in the terminal.
So far, we’ve only learned one function:
type(). Let’s try out another, called
len(), which returns the number of items in a list or the number of characters in a string.
First, let’s “comment out” the
print(books) statement with the hastag
#, which tells python to ignore that line of code. Then, type out the following lines in your loop.py file:
books = ['Gender Trouble', 'Cruising Utopia', 'Living a Feminist Life'] # print(books) list_length = len(book) print(list_length)
Let’s take apart this unfamiliar line of code:
list_length = len(book)
- First, we have saved a list of books to the variable,
- Second, look to the next line this statement.
list_length = len(book)takes the
booksvariable from the previous line as an argument (or input data) for the
len()function. That’s why
booksis within the parenthesis. This syntax means that python will run the
len()function on the items in
books. Then, it sets the result of that process to a new variable, called
- Finally, we print the
list_lengthvalue. This might appear a bit complex at first, but if you read the line slowly you should be able to connect the dots. Notice that when you run the code above, you don’t see the
bookslist printed out. That’s because that line has become a comment. If you put a
#(hash or pound) at the beginning of a line, that line will be ignored.
A useful property of a list is the list index. This allows you to pick out an item from within the list by a number starting from zero:
print(books) # Gender Trouble print(books) # Cruising Utopia
Note that the first item in the list is
item. The second item is
item. That’s because counting in Python, and in almost all programming languages, starts from
Additionally, you can print out the last item in a list using negative numbers, where
-1 denotes the last item in the list:
print(books[-1]) # Living a Feminist Life
There are many things you can do with list indexing, like slicing. Slicing consists of taking a section of a list, using the list index to pick out a range of list items. For example, you could take out the first two items of a list with a slice that begins with
0 and ends with
The slice syntax consists of square brackets, start point and end point, and a colon to indicate the gap in between. This should print out the first two items of your list.
Note a couple of things. First, the start point is inclusive, meaning that Python will include the
 item in your range, and the end point is exclusive, so Python won’t print the
 item. Instead, it will print everything up until that
For ultimate brevity, you can also write this expression as:
The empty value before the colon allows Python to assume the range starts at the first list item, at
. You can also end the slice with
:, if you want the list range to include all subsequent items until the end of the list. The example below will print everything from the second item to the end of the list.
With a list that contains three items total, list slicing might not seem very impressive right now. However, this will become a powerful tool once we get to more sophisticated text analysis and start to encounter lists that contain hundreds (or thousands!) of items.
Challenges for lesson 6
Create a new list of books in the REPL, with at least 5 books in your list. Make sure the total number of books in the list is an odd number. How do you get python to print out the book in the middle of the list? What about the three books in the middle? Remember that the first value in a slice is inclusive, and the final value is exclusive.
Assignment: ChallengeCreate a new list of books in the REPL, with at least 5 books in your list. Make sure the total number of books in the list is an odd number. How do you get python to print out the book in the middle of the list? What about the three books in the middle? Remember that the first value in a slice is inclusive, and the final value is exclusive.
>>> books = ['Gender Trouble', 'Cruising Utopia', 'Living a Feminist Life', 'Radiant Textuality', 'The Undercommons'] >>> books # ['Living a Feminist Life'] >>> books[1:4] # ['Cruising Utopia', 'Living a Feminist Life', 'Radiant Textuality']
How would you get Python to print the length of the last book in the list? Hint: this number reflects the length of the string which is the last item in the list. Choose the correct expression from the options below.
How would you get Python to print the length of the last book in the list? Hint: this number reflects the length of the string which is the last item in the list. Choose the correct expression from the options below.(Select one of the following)
Terms Used in Lesson
Can you define the terms below? Hover over each of them to read a preview of the definitions.
len() is a function that calculates the length of the object within the parenthesis. For a string, it will count the number of characters: len("hello") will return 5, and len("hi there") will return 8, because …
A list is a data type, like string, integer, float, and Boolean. A list holds items in a sequence, separated by commas and contained within square brackets. They can hold different types of data, like …
List indexing is a foundational tool for working with lists. It allows one to access items from a list by specifying the item according to its location within the list. The item location is represented …