Programming Examples

Python program to create a new dictionary D2 having same keys as D1 but values as the sum of the list elements


A dictionary D1 has values in the form of lists of numbers. Write a program to create a new dictionary D2 having same keys as D1 but values as the sum of the list elements.

 

e.g.

 

D1 = {'A': [1, 2, 3], 'B': [4, 5, 6]}

Then

D2 is {'A':6, 'B': 15}

Solution

dic1 = eval(input("Enter dictionary : "))
dic2 = {}
for i in dic1 :
      lst = dic1[ i ]
      sum_of_num = sum( lst )
      dic2 [ i ] = sum_of_num
print(dic2)
Output

Enter dictionary :  {"list 1":[1,2,3,4],"list 2":[4,5,6,7],"list 3":[4,2,3]}

{'list 1': 10, 'list 2': 22, 'list 3': 9}

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad