Reverse values and decorate keys

EasyDictionary Programs · 93 of 94

Write a Python program to reverse each string value and put underscores around each key.

Advertisement
Input
{"a": "Python", "b": "Programming", "c": "Learning"}
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}

Approach

  1. Dict comprehension with an f-string key and [::-1] value.

Solution

Python
d = {"a": "Python", "b": "Programming", "c": "Learning"}print({f"_{k}_": v[::-1] for k, v in d.items()})
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}
Advertisement

Learn the concept

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

More dictionary programs

← All 94 Dictionary Programs