In this python dictionary program, we will reverse string value in the dictionary and add an underscore before and after the Keys.
Steps to solve the program
- Take a dictionary with string values as input and create an empty dictionary.
- Use for loop to iterate over keys and values of the dictionary.
- Add an underscore before and after the key using string concatenation.
- Add that key to the empty dictionary and its value also.
- Print the output.
D1 = {'a':'Python','b':'Programming','c':'Learning'}
D2 = {}
for k,v in D1.items():
D2['_'+k+'_'] = v[::-1]
print(D2)
Output :
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}
sum unique elements from dictionary list values.