Dictionary keyed by first and last letters

EasyFunction Programs · 25 of 39

Write a Python function that maps the first and last letter of each word to the word.

Advertisement
Input
"Python is easy to learn"
Output
{'Pn': 'Python', 'is': 'is', 'ey': 'easy', 'to': 'to', 'ln': 'learn'}

Approach

  1. Key is w[0] + w[-1].

Solution

Python
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'}
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More function programs

← All 39 Function Programs