Can you explain the example?

Hello, I don’t understand how the second example would result in a 6. Anyone has any idea? Thank you.

if the key is an even number, add the values together. For the second function call, all the dictionaries have even keys (10, 100, 1000) so you get the sum of all the values (1 + 2 + 3)

4 Likes

I got that part, but why is total = 0 += my_dictionary [ <<< why is array of key: here?>>] Shouldn’t it be adding the first part of the array such as key:value? 10 + 100 + 1000

I am not sure, but I think I found the answer to my own question. I think the array assigned the key in the dictionary as placement to the value. I am I right?

just because we use square brackets, doesn’t make it an array/list. Square brackets are also use to access list element by index:

my_list[index]

or to get a value from a dictionary with a key:

my_dict[key]
1 Like