Can someone explain me what happens when we pass dictionary to sum function in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain me what happens when we pass dictionary to sum function in python?

Example code print(sum(1:2, 2:4)

7th Dec 2018, 2:58 AM
nikhil mhatre
nikhil mhatre - avatar
1 Answer
+ 5
When we iterate over a dictionary, like for k in my_dict: print(k) we just iterate over its keys. So when we do sum(my_dict), we just get the sum of its keys. print(sum({1:2, 2:4})) # Output: 3 (1+2)
7th Dec 2018, 3:15 AM
Kishalaya Saha
Kishalaya Saha - avatar