Things get more interesting with lists. See if you can solve the questions below. Remember to run the following cell first.
from learntools.core import binder; binder.bind(globals())
from learntools.python.ex4 import *
print('Setup complete.')
Setup complete.
Complete the function below according to its docstring.
def select_second(L):
"""Return the second element of the given list. If the list has no second
element, return None.
"""
return None if len(L) < 2 else L[1]
# Check your answer
q1.check()
Correct
#q1.hint()
q1.solution()
Solution:
def select_second(L):
if len(L) < 2:
return None
return L[1]
You are analyzing sports teams. Members of each team are stored in a list. The Coach is the first name in the list, the captain is the second name in the list, and other players are listed after that. These lists are stored in another list, which starts with the best team and proceeds through the list to the worst team last. Complete the function below to select the captain of the worst team.
def losing_team_captain(teams):
"""Given a list of teams, where each team is a list of names, return the 2nd player (captain)
from the last listed team
"""
# COACH = 0
CAPTAIN = 1
return teams[-1][CAPTAIN]
# Check your answer
q2.check()
Correct
#q2.hint()
q2.solution()
Solution:
def losing_team_captain(teams):
return teams[-1][1]
The next iteration of Mario Kart will feature an extra-infuriating new item, the Purple Shell. When used, it warps the last place racer into first place and the first place racer into last place. Complete the function below to implement the Purple Shell's effect.
def purple_shell(racers):
"""Given a list of racers, set the first place racer (at the front of the list) to last
place and vice versa.
>>> r = ["Mario", "Bowser", "Luigi"]
>>> purple_shell(r)
>>> r
["Luigi", "Bowser", "Mario"]
"""
racers[0], racers[-1] = racers[-1], racers[0]
# Check your answer
q3.check()
Correct
#q3.hint()
q3.solution()
Solution:
def purple_shell(racers):
# One slick way to do the swap is x[0], x[-1] = x[-1], x[0].
temp = racers[0]
racers[0] = racers[-1]
racers[-1] = temp
What are the lengths of the following lists? Fill in the variable lengths
with your predictions. (Try to make a prediction for each list without just calling len()
on it.)
a = [1, 2, 3]
b = [1, [2, 3]]
c = []
d = [1, 2, 3][1:]
# Put your predictions in the list below. Lengths should contain 4 numbers, the
# first being the length of a, the second being the length of b and so on.
lengths = [3, 2, 0, 2]
# Check your answer
q4.check()
Correct:
[2, 3]
counts as a single item. It has one item before it. So we have 2 items in the list[2, 3]
, which has length 2.# line below provides some explanation
q4.solution()
Solution:
[2, 3]
counts as a single item. It has one item before it. So we have 2 items in the list[2, 3]
, which has length 2.We're using lists to record people who attended our party and what order they arrived in. For example, the following list represents a party with 7 guests, in which Adela showed up first and Ford was the last to arrive:
party_attendees = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
A guest is considered 'fashionably late' if they arrived after at least half of the party's guests. However, they must not be the very last guest (that's taking it too far). In the above example, Mona and Gilbert are the only guests who were fashionably late.
Complete the function below which takes a list of party attendees as well as a person, and tells us whether that person is fashionably late.
def fashionably_late(arrivals, name):
"""Given an ordered list of arrivals to the party and a name, return whether the guest with that
name was fashionably late.
"""
arrival_position = arrivals.index(name)
if arrival_position == len(arrivals)-1:
return False
if arrival_position >= len(arrivals)/2:
return True
return False
# Check your answer
q5.check()
Incorrect: Expected return value of False
given arrivals=['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
, name='Adela'
, but got True
instead.
q5.hint()
q5.solution()
Hint: Use the index method to find when the person arrived. Check whether that is a fashionably late spot given the list length (len
). Think about 0-indexing
Solution:
def fashionably_late(arrivals, name):
order = arrivals.index(name)
return order >= len(arrivals) / 2 and order != len(arrivals) - 1
That's it for lists and tuples! Now you have the baseline knowledge to learn about loops, which is where lists and tuples get really interesting.
Have questions or comments? Visit the Learn Discussion forum to chat with other Learners.