Page 50 -
P. 50
additional data
§
Let’s take a bit of time to try and work out which strategy to use when adding data to your list in
this case.
Given the following list-creation code:
movies = ["The Holy Grail", "The Life of Brian", "The Meaning of Life"]
1 You were to work out the Python code required to insert the numeric year data into the preceding
list:
Insert the first year
BEFORE the second list
item. movies.insert(1, 1975)
Did you get the math right?
After the first insertion, the
Insert the second year
list grows, so you have to take
BEFORE the fourth list item. movies.insert(3, 1979) that into consideration when
working out where to do the
second insert.
movies.append(1983)
Then append the last year to
the end of the list.
2 You were also to write the Python code required to recreate the list with the data you need all in
one go:
movies = ["The Holy Grail", 1975,
Assign all your data to the “movies” "The Life of Brian", 1979,
identifier. What was previously there is "The Meaning of Life", 1983]
replaced.
In this case, which of these two methods do you think is best? (You were to circle your choice.)
Yes, method 2 seems the better
1 or 2 option here…that is, for a small
list like this. Also, there’s no
tricky counting to do.
14 Chapter 1