Problem overview
The method shout gets a list words of lowercase words and returns a new list where every word is shouted: written in capital letters, with ! after it. So "hey" becomes "HEY!". The order stays the same, and a repeated word is shouted each time. For ["hey", "you"] the answer is ["HEY!", "YOU!"].
Approach
Every word turns into exactly one new word, in the same position, with no word skipped. That is the shape of a list comprehension without an if: one rule applied to each item. The rule has two parts. The string method upper() gives the word in capital letters, and + "!" adds the mark at the end.
Strings can't be changed in place in Python, so word.upper() doesn't touch word. It returns a new string, and the comprehension collects those new strings in a new list.
Step by step
- Build
shoutedwith a comprehension: for eachwordinwords, takeword.upper() + "!". - Return
shouted. The input listwordshasn't changed.
The solution in Python
class Solution:
def shout(self, words: List[str]) -> List[str]:
shouted = [word.upper() + "!" for word in words]
return shouted
The same loop written out is: start with an empty list, go through words, and append word.upper() + "!" each time. The comprehension is the short form of exactly that.
Why not change the list in place
The statement asks for a new list, and the comprehension gives exactly that. The caller's list is left alone, which is the safer habit: code that called shout(words) can keep using words afterwards and still see the lowercase originals. Also, a function that returns a value is easy to test, because you can compare its result to the expected list directly.
Testing
["hey", "you"] -> ['HEY!', 'YOU!']
["go"] -> ['GO!']
["no", "no", "no"] -> ['NO!', 'NO!', 'NO!']
After calling the method on ["hey"], the original list is still ['hey'].
Other ways to build the string
An f-string gives the same result: f"{word.upper()}!" produces 'HEY!' for "hey". Both are fine; the lesson uses + because it is the shortest way to read "add a ! after it". Be careful with similar methods: capitalize() only makes the first letter a capital, so "hey".capitalize() + "!" gives 'Hey!', which is not shouting. And leaving out the "!" gives ['HEY', 'YOU'], which is only half of the job.
Edge cases
A list with one word gives a list with one shouted word. A repeated word is shouted each time, because the comprehension handles each position on its own and never looks at the other words. The words have only the letters a to z, so upper() has no surprises here: every letter has a capital, and nothing else needs care. The result is always exactly as long as words.
Common mistakes
- Writing
word.upperwithout the parentheses. That gives the method itself, not the capital letters, and adding"!"to it stops with aTypeError. - Calling
words.upper()on the whole list, which stops withAttributeError: 'list' object has no attribute 'upper'.upper()belongs to a single string, so apply it toword. - Putting
"!"in front, or adding it to the list instead of to each word. - Printing instead of returning. The method must return the new list.
