Head First – Python: 1st exercise

 

page 13 in o’reilly’s head first python

the question was to convert a list of movie titles into a list which contains movie titles and the movie release years.

what i have:
["the holy grail", "the life of brian", "the meaning of life"]

what i want:
["the holy grail", 1975, "the life of brian", 1979, "the meaning of life", 1983]

my solution:

#!/usr/bin/python

#create list for movie titles
movies = ["the dark knight", "donnie darko"]

#extend the list with two further titles
for i in ["8 mile", "game of thrones"]:
    movies.append(i)

#print how many titles are in the list
print("list contains", len(movies), "entries")

#print the list
print("actual list:", movies)

#create list with the movie release years
years = [2008, 2009, 2010, 2011]
#set first insert index
count = 1
#insert year as often as years in the years list
for year in years:
    #insert year
    movies.insert(count, year)
    #increment for two
    count += 2

#print result (new list)
print(movies)

and yes.. i’m fully aware that this are not all movies.. and yes.. the years are wrong too.. and yes.. it wasn’t necessary to append two of the movies in a second step.. bite me!

Leave a Reply

Your email address will not be published. Required fields are marked *

*


one × 4 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>