5. Lists#

Exercise 5.1#

The list named titles below brings together a number of book titles. Print a sentence that gives information about the number of books in this list of titles. Next, create a new list named last_two containing only the last two titles in this series.

titles = ["Philosopher's Stone","Chamber of Secrets","Prisoner of Azkaban",
          "Goblet of Fire","Order of the Phoenix","Half-Blood Prince","Deathly Hallows"]

print(f'This book series consists of {len(titles)} titles.')

last_two = titles[-2:]
print(last_two)

## An alternative solution with the exact same effect
last_two = titles[5:]
print(last_two)

Exercise 5.2.#

Create the following list:

colours = ['green','blue','red']

Add the colours ‘orange’ and ‘yellow’ to this list.

colours = ['green','blue','red']

colours.append('orange')
colours.append('yellow')

# Let's see the results
print(colours)

Exercise 5.3.#

The list named years brings together a number of years. Use the keywords for and if to select the years from this list that are in the 18th century. Remember that you can combine multiple Boolean expressions using and.

years = [ 1756, 1575, 2002, 1984 ,1626 ,1791, 1714, 1873, 1991 ]

# Print years that are in the 18th century
for year in years:
    if year > 1700 and year < 1800:
        print( f'{year} is in the 18th century.') 

Exercise 5.4.#

We have two lists of numbers and would like to know which numbers in the first list are not in the second list.

Write code which can extract those numbers from the first list that are not in the second list.

first = [4, 9, 1, 17, 11, 26, 28, 54, 63] 
second = [8, 9, 74, 21, 45, 11, 63, 28, 26]

numbers_not_in_second = []

# Add the numbers from `first` that are not in `second` to `numbers_not_in_second`
for f in first:
    if f not in second:
        numbers_not_in_second.append(f)

print(numbers_not_in_second)

Exercise 5.5.#

The list below contains the titles of first twelve plays written by William Shakespeare.

plays = [ 'Comedy of Errors' ,
'Henry VI, Part I' ,
'Henry VI, Part II' ,
'Henry VI, Part III' ,
'Richard III' ,
'Taming of the Shrew' ,
'Titus Andronicus' ,
'Romeo and Juliet' ,
'Two Gentlemen of Verona' ,
'Love\'s Labour\'s Lost' ,
'Richard II' ,
'Midsummer Night\'s Dream' ]```

Add the following two titles to this list:
 
* Macbeth
* Othello

Next, count the number of plays in this list and print the titles of the first and the last plays in the list, using the index of these items. 
Print the full list in alphabetical order using the ‘for’ keyword.
plays = [
    'Comedy of Errors',
    'Henry VI, Part I',
    'Henry VI, Part II',
    'Henry VI, Part III',
    'Richard III',
    'Taming of the Shrew',
    'Titus Andronicus',
    'Romeo and Juliet',
    'Two Gentlemen of Verona',
    'Love\'s Labour\'s Lost',
    'Richard II',
    'Midsummer Night\'s Dream'
]

Add the following two titles to this list:

  • Macbeth

  • Othello

# Use my_list.append(item_to_add) to add items to a list
plays.append('Macbeth')
plays.append('Othello')

Next, count the number of plays in this list.

# Use the len(my_list) function to count the items in the list
print( f'This list contains {len(plays)} plays.' )

Print the titles of the first and the last plays in the list, using the index of these items.

print( f"The first play in the list is '{plays[0]}'." )
print( f"The last play in the list is '{plays[-1]}'." )

Print the full list in alphabetical order using the for keyword.

# Use sorted(my_list) to get the list's element in sorted order
for p in sorted(plays):
    print(p)

Exercise 5.6.#

We have a list of websites and are interested to find the Dutch ones.

Write some code to select the Dutch websites from this list. Tip: You may want to reuse some of the code developed for exercise 3.4.

websites = [
    'https://www.universiteitleiden.nl',
    'https://www.stanford.edu',
    'https://www.uu.nl',
    'http://www.ox.ac.uk',
    'https://www.rug.nl',
    'https://www.hu-berlin.de',
    'https://www.uva.nl'
]

# Print the URLs that belong to Dutch institutions
for url in websites:
    # Extract the top-level domain, i.e. everything after the final dot
    # You can leave out the end index of the 'slice' to get everything until the end of the string
    top_level_domain = url[ url.rindex('.') + 1 : ]
    # Dutch institutions have a '.nl' website
    if top_level_domain == 'nl':
        print(url)

Exercise 5.7.#

We have a quote from E.M. Forster’s A Room with a View below and want to get a few statistics about it.

quote = '''We cast a shadow on something wherever we stand, and it is no good 
moving from place to place to save things; because the shadow always follows. Choose 
a place where you won’t do harm - yes, choose a place where you won’t do very much 
harm, and stand in it for all you are worth, facing the sunshine.'''

First we’d like to know the total number of words. Here we say that the words are all the strings separated by spaces. That means “won’t” is a word in the quote, as well as “stand,” (including the comma). It depends on the context of the research if this is okay, or that you need to further process the words before you count them.

We can convert a string to a list of words, using the split() method. This method can convert a string into a list, based on a character or space that occurs in this string. This split() method must be added after the the string variable, using a dot, as follows:

quote.split(string_to_split_on)
quote.split()
# Not specifying what to split on is the same as splitting by a space, like this:
quote.split(' ')

Use this split() method to give information about the total number of words in this quote.

# First we split the string into a list of words
words = quote.split()

# Now we can count the number of words, which is the number of elements in our list
print( f'The quote contains {len(words)} words.' )

Print the last word in the quote, and count the number of occurrences of the word “place”.

# The last word is the word with index -1 in our list
print( f'The last word is \'{words[-1]}\'.' )
# Note how it includes the '.'?
# In many cases, you will want to make sure that you remove such punctuation before processing text.
print( f'The last word is \'{words[-1].strip(".")}\'.' )

# How many times does "place" occur in the quote?
print(f"There are {words.count('place')} occurrences of the word 'place'.")

# If you want to count "PlAcE" and "place" as the same, you need a more elaborate program.
count = 0

for w in words:
    if w.lower() == 'place':
        count+= 1

print( f"There are {count} occurrences of the word 'place', counted in case-insensitive way.")