0%
Checking the code runner…

Instructions

Lesson 4 of 4

Lists

A list holds several values in order, inside square brackets:

Lists can grow with .append():

Looping over a list

for works directly on lists, and if lets you choose what to do with each item:

Exercise: Longest word

Write longest_word(words) that returns the longest string in the list. If several are tied, return the first one.

Your turn!

Write your code in and press Submit answer.

  • print(longest_word(["ale", "mushrooms", "pie"])) → mushrooms
  • print(longest_word(["one", "two", "six"])) → one
  • plus 2 hidden checks

Help

Hint

Keep track of the best word so far. Start with best = words[0], then for each word, replace best only if the new word is strictly longer.

Pass the exercise to complete this lesson.

Terminal
Press Run to see your output here.