Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Python Comprehensions!
You have completed Python Comprehensions!
Preview
How did you go? In this video I’ll walk through what I did to complete these challenges.
Solutions
Challenge 1
leap_years = [year for year in range(1901, 2001) if year%4==0]
Challenge 2
the_simpsons = [f"{character} Simpson" for character in characters if character in simpsons]
Challenge 3
word_lengths = {word: len(word) for word in words if len(word)>=5}
Challenge 4
ages_next_year = {person: age+1 if person!='Danny' else age for (person, age) in ages.items()}
Challenge 5
vowels = {letter for letter in statement.lower() if letter in 'aeiou'}
Challenge 6
overlap = {food for food in fruits if food in vegetables}
Challenge 7
def format_name(honorific, candidate):
return honorific + " " + candidate.get('name').split()[-1]
graduates = [format_name("Professor", candidate) if candidate.get('score')>=90 else format_name("Doctor", candidate) for candidate in phd_candidates if candidate.get('score') >=70]
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Here are my solutions, but remember
that these are just my solutions, and
0:00
there could be a few different ones.
0:02
If yours differs to mine, don't worry, but
0:06
do feel free to share your
solutions in the forums!
0:08
I'll be walking through all the solutions
before running the file for the sake
0:12
of easy viewing, but feel free to be
running and testing your code as you go.
0:18
For the leap year challenge,
0:23
I started with square brackets
because I know I'm creating a list.
0:25
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up