Mapping & Filtering
Lesson 4 of 9 in Coddy's Python List Comprehension course.
We can also use both filtering and mapping at the same time!
Reminder of the syntax,
lst = [expression for item in iterable if condition]It is important to note that the condition in list comprehension is executed on each item in the iterable before the expression. This means that every item in the iterable is checked against the condition, and if it is True, the expression is executed and the item is added to the new list.
Challenge
EasyGiven a list of strings words, calculate a new list where only the first letter of each word is stored if the word is palindrome.
Store the new list in a variable named new_words.
A palindrome is a string that remains the same even when read backwards, for example "php".
Try it yourself
words = eval(input()) # Don't change this line