Python Practice › Function Programs › Program 25
Dictionary keyed by first and last letters
Write a Python function that maps the first and last letter of each word to the word.
Input
"Python is easy to learn"
Output
{'Pn': 'Python', 'is': 'is', 'ey': 'easy', 'to': 'to', 'ln': 'learn'}Approach
- Key is
w[0] + w[-1].
Solution
def word_map(text): return {w[0] + w[-1]: w for w in text.split()} print(word_map("Python is easy to learn"))Output
{'Pn': 'Python', 'is': 'is', 'ey': 'easy', 'to': 'to', 'ln': 'learn'}Learn the concept
New to this topic? Read the lesson, then come back.