Reverse string value in the dictionary and add an underscore before and after the Keys.

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
  1. Take a dictionary with string values as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. Add an underscore before and after the key using string concatenation.
  4. Add that key to the empty dictionary and its value also.
  5. 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.

Leave a Comment