For a recent Python homework assignment, we were assigned to create a function that would return words in a list that start with 'd'. Here's the relevant code:
def filter_words(word_list, letter):
'''
INPUT: list of words, string
OUTPUT: list of words
Return the words from word_list which start with the given letter.
Example:
>>> filter_words(["salumeria", "dandelion", "yamo", "doc loi", "rosamunde",
"beretta", "ike's", "delfina"], "d")
['dandelion', 'doc loi', 'delfina']
'''
letter_list = []
for word in word_list:
if word[0] == letter:
letter_list.append(word)
return letter_list
The above nested if statement that I wrote works when I run the code, which I'm happy about (:D); however, in trying to become more pythonic and astute with the language, I found a very helpful article on why Lambda functions are useful and how to possibly solve this same challenge with a lambda, but I couldn't figure out how to make it work in this case.
I'm asking for guidance on how to potentially write my above nested if statement as a lambda function.