Lists#
Learning Objectives
Questions:
How can I store multiple items?
Objectives:
Explain why programs need collections of items.
Write programs that create lists, index them, slice them, and modify them through assignment and method calls.
What are lists?#
A list is a Python data type that stores many items in a single structure.
Scenario: You have set up an thermometer to do temperature measurements in a storage room for rare books.
Doing calculations with a hundred variables called temperature_001
, temperature_002
, etc.,
would be at least as slow as doing them by hand.
Use a list to store many items together.
List items are contained within square brackets
[...]
.List items are separated by commas
,
.List items are ordered by their index number.
Use
len()
to find out how many items are in a list.
temperatures = [17.3, 17.5, 17.7, 17.5, 17.6]
print('temperatures:', temperatures)
print('length:', len(temperatures))
Show code cell output
temperatures: [17.3, 17.5, 17.7, 17.5, 17.6]
length: 5
Indexing lists#
We can use an item’s index to fetch it from a list.
Just like with strings, we can use index to find a given item in a list.
And just like with strings, the elements in a list are 0-indexed. (See lesson about Data Types.)
print('zeroth item of temperatures:', temperatures[0])
print('fourth item of temperatures:', temperatures[4])
Show code cell output
zeroth item of temperatures: 17.3
fourth item of temperatures: 17.6
Slicing lists#
Just like with strings, we can use indexing syntax to slice lists.
If l
is a list, an expression of the form l[start:stop]
returns the portion of l
starting with position start
, and up to but not including position stop
.
Take a look at the example with the list of temperatures:
print(temperatures)
print(temperatures[1:4])
Show code cell output
[17.3, 17.5, 17.7, 17.5, 17.6]
[17.5, 17.7, 17.5]
Replacing list items#
Lists’ items can be replaced by assigning to them.
You can use an index expression on the left of the assignment operator (=
) to replace a value:
temperatures[0] = 16.5
print('temperatures is now:', temperatures)
Show code cell output
temperatures is now: [16.5, 17.5, 17.7, 17.5, 17.6]
Appending to lists#
Appending items to a list lengthens it.
You can use list_name.append
to add items to the end of a list:
print('temperatures is initially:', temperatures)
temperatures.append(17.9)
temperatures.append(18.2)
print('temperatures has become:', temperatures)
Show code cell output
temperatures is initially: [16.5, 17.5, 17.7, 17.5, 17.6]
temperatures has become: [16.5, 17.5, 17.7, 17.5, 17.6, 17.9, 18.2]
.append()
is a method of lists.
* A method is similar to a function, but tied to a particular object.
You can use object_name.method_name
to call methods and help(list)
for a preview.
Table of List Methods
For more in-depth information, see the Python Documentation.
Method |
Description |
---|---|
|
Adds an element at the end of the list |
|
Removes all the elements from the list |
|
Returns a copy of the list |
|
Returns the number of elements with the specified value |
|
Adds the elements of a list (or any iterable), to the end of the current list |
|
Returns the index of the first element with the specified value |
|
Adds an element at the specified position |
|
Calculates the maximum of all the elements of the list |
|
Calculates the minimum of all the elements of the list |
|
Removes the element at the specified position |
|
Removes the first item with the specified value |
|
Reverses the order of the list |
|
Sorts the list |
Deleting list items#
We can use del
to remove items from a list entirely.
del list_name[index]
removes an item from a list and shortens the list.del
is not a function or a method, but a statement in the Python language.
numbers = [2, 3, 5, 7, 11]
print('numbers before removing last item:', numbers)
del numbers[4]
print('numbers after removing last item:', numbers)
Show code cell output
numbers before removing last item: [2, 3, 5, 7, 11]
numbers after removing last item: [2, 3, 5, 7]
Warning
If we do not add an index, the whole list will be deleted!
I.e., del list_name
will delete the whole list_name
.
Empty lists#
The empty list contains no items.
Use []
on its own to represent a list that does not contain any items.
“The zero of lists.”
This is helpful as a starting point for collecting values (which we will see in the next episode about For Loops.
Lists may contain items of different data types#
A single list may contain numbers, strings, and anything else.
goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
print(goals)
Show code cell output
[1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']
Lists are mutable#
In Python, mutable data types, such as lists, are data structures that can be modified or changed after they are created. This means you can add, remove, or modify elements within a list without creating a new list.
For example, you can append new items, insert items at specific positions, remove items, and change the values of existing items in a list. This behavior contrasts with immutable data types, like integers, floats, and strings, where once created, their contents cannot be altered without creating a new object. (This was briefly mentioned in the lesson about Variables and Assignment.)
Character strings are immutable#
Remember that you can get single characters from a character string using indexes in square brackets:
book_title = 'To Kill a Mockingbird'
print('zeroth character:', book_title[0])
print('third character:', book_title[4])
Show code cell output
zeroth character: T
third character: i
Lists and character strings are both collections.
But!
You cannot alter the characters in a string after it has been created.
Immutable: cannot be changed after creation. E.g., strings.
In contrast, lists are mutable: they can be modified in place.
Python considers the string to be a single value with parts, not a collection of values.
book_title[0] = 'N'
Show code cell output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[9], line 1
----> 1 book_title[0] = 'N'
TypeError: 'str' object does not support item assignment
Notice the difference between overwriting and changing values
In Python, when you use the assignment operator (=
) with a variable (and not a variable index!), it does not change the original value of the variable.
Instead, it replaces the current value of the variable with the new value on the right-hand side of the assignment.
book_title = 'To Kill a Mockingbird'
print(book_title)
book_title = '1984'
print(book_title)
Show code cell output
To Kill a Mockingbird
1984
In the code above, the variable book_title
initially holds the value ‘To Kill a Mockingbird’, but when the second line is executed, it is overwritten with the new value ‘1984’.
The old value ‘To Kill a Mockingbird’ is effectively discarded, and book_title
now contains ‘1984’.
In contrast, when assigning to a list index:
book_titles = ['To Kill a Mockingbird', '1984']
print(book_titles)
book_titles[0] = 'Romeo and Juliet'
print(book_titles)
Show code cell output
['To Kill a Mockingbird', '1984']
['Romeo and Juliet', '1984']
We do not overwrite the variable book_titles
but rather we change the content of the zeroth index.
Thus, the difference between immutable and mutable data types.
Indexing beyond the end#
Indexing beyond the end of the collection is an error. Therefore, python reports an IndexError
if we attempt to access a value that does not exist.
This is a kind of runtime error.
print('99th book of book_titles is:', book_titles[99])
Show code cell output
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[12], line 1
----> 1 print('99th book of book_titles is:', book_titles[99])
IndexError: list index out of range
Exercises#
Exercise 1: Fill in the blanks#
Fill in the blanks so that the program below produces the output shown.
values = ____
values.____(1)
values.____(3)
values.____(5)
print('first time:', values)
values = values[____]
print('second time:', values)
first time: [1, 3, 5]
second time: [3, 5]
Solution
values = []
values.append(1)
values.append(3)
values.append(5)
print('first time:', values)
values = values[1:3]
print('second time:', values)
first time: [1, 3, 5]
second time: [3, 5]
Exercise 2: How large is a slice?#
If ‘low’ and ‘high’ are both non-negative integers, how long is the list values[low:high]
?
Solution
The list’s length would be equal to high
minus low
.
Exercise 3: Working with the end#
What does the following program print?
harrypotter1 = "the philosopher's stone"
print(harrypotter1[-1])
Show code cell output
e
How does Python interpret a negative index?
If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent?
If
harrypotter1
is a list, what doesdel harrypotter1[-1]
do?How can you display all elements but the last one without changing
harrypotter1
?
(Hint: you will need to combine slicing and negative indexing.)
Solution
A negative index begins at the final element.
-(N)
corresponds to the first index, which is the [0] index.It removes the final element of the list.
You could do the following:
print(harrypotter1[0:-1])
Exercise 4: Stepping through a list#
What does the following program print?
song = 'despacito'
print(song[::2])
print(song[::-1])
Show code cell output
dsaio
oticapsed
If we write a slice as
low:high:stride
, what doesstride
do?What expression would select all of the even-numbered items from a collection?
Solution
stride
refers to the step size between each index in a slice operation, allowing you to select elements at regular intervals. Strides can be positive to move forward through a list or negative to reverse the order of selection and traverse backwards by the specified interval.song[1::2]
Exercise 5: Slicing#
What does the following program print?
song = 'blinding lights'
print(song[1:2])
print(song[-1:3])
print(song[0:20])
Show code cell output
l
blinding lights
Solution
Index
[1]
is the second characterl
(remember, Python is zero indexed). It goes up to but not including index[2]
. Thus, onlyl
is printed.There is no element after index
[-1]
, so an empty line is printed.There is no index
[20]
, so the entire string is captured and printed.
Exercises 6: Sort and sorted#
What do these two programs print?
In simple terms, explain the difference between sorted(letters)
and letters.sort()
.
# Using sorted()
letters = list('gold')
result = sorted(letters)
print('letters is', letters, 'and result is', result)
Show code cell output
letters is ['g', 'o', 'l', 'd'] and result is ['d', 'g', 'l', 'o']
# Using .sort()
letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)
Show code cell output
letters is ['d', 'g', 'l', 'o'] and result is None
Solution
sorted(letters)
returns a sorted copy of the list without changing the original list, while letters.sort()
sorts the original list but does not return anything, i.e. returns None
.
Exercise 7: Copying (or not)#
What do these two programs print?
In simple terms, explain the difference between new = old
and new = old[:]
.
old = list('gold')
new = old # simple assignment
new[0] = 'D'
print('new is', new, 'and old is', old)
Show code cell output
new is ['D', 'o', 'l', 'd'] and old is ['D', 'o', 'l', 'd']
old = list('gold')
new = old[:] # assigning a slice
new[0] = 'D'
print('new is', new, 'and old is', old)
Show code cell output
new is ['D', 'o', 'l', 'd'] and old is ['g', 'o', 'l', 'd']
Solution
new = old
is assigning old
to new
.
This means that the two variables both point to the same value.
Thus, changing the contents of either variable will affect the other.
In contrast, new = old[:]
is a slice assignment, which will only return a copy of old
.
Exercise 8: From strings to lists and back#
Given this:
print('string to list:', list('tin'))
print('list to string:', ''.join(['g', 'o', 'l', 'd']))
print('list to string:', '-'.join(['g', 'o', 'l', 'd']))
Show code cell output
string to list: ['t', 'i', 'n']
list to string: gold
list to string: g-o-l-d
Explain in simple terms what
list('some string')
does.What does
'-'.join(['x', 'y'])
generate?
Solution
It creates a list of the
some string
’s characters as elements.It creates a string composed of
x
andy
, separated by a hyphen character(-
).
Key points#
A list stores many items in a single structure.
Use an item’s index to fetch it from a list.
Use slicing to extract part of a list.
Lists’ items can be replaced by assigning to them.
Appending items to a list lengthens it.
Use
del
to remove items from a list entirely.The empty list contains no items.
Lists may contain items of different types.
Lists are mutable.
Indexing beyond the end of the collection is an error.