How to solve this challenge with list comprehension

Might be better to run loops, but I thought I should share how one can solve it in one line using the built-in sum() function on a list comprehension.

def sum_even_keys(d):
  return sum([x[1] for x in d.items() if x[0] % 2 == 0])
# Uncomment these function calls to test your  function:
print(sum_even_keys({1:5, 2:2, 3:3}))
# should print 2
3 Likes

you can use a generator so that the list doesn’t have to be constructed in memory.

1 Like

does use both
and generators are slower :stuck_out_tongue:

1 Like

ah, x[0] and x[1] i see. Oops, i am so used to see:

for key, value in d.items()

but take less memory? What else is the point of them?

1 Like

Yeah constant memory
they can also be infinite
they can pause and resume execution of something
not all data might be needed
the start of data might be needed before all of it is available
instead of pushing data to the consumer, the consumer can pull from the producer

I don’t have my head fully (not even close) wrapped around it, no clue what the full extent of their use is.

Any kind of pipe is super cool

1 Like

So x[0] is the first key and x[1] is the first value? In that case x[2] will be second key, x[3] second value etc.?

No. Doing:

d = {1:5, 2:2, 3:3}
print(d.items())

or:

d = {1:5, 2:2, 3:3}
for x in d.items():
    print(x)

might help to generate some insight

if something is unclear in the code, learning to analyze the code can be helpful (like i just done above)

I got some funky output on the first one, my print statement was:

dict_items([(1, 5), (2, 2), (3, 3)])

Where did the “dict_items” come from?

But nontheless, I get now how using items will create a list containing pairs of keys and values. But the code were talking about is still unclear for me. The way I see it it’s gonna check only first pair from the list (x[0] and x[1]) and just return x[1] (a value from first pair). x[1] and x[0] specifically point to the first and second index of the list so all the other indexes of that list will remain unchecked.

there aren’t many possible explanation, given the only thing you do is calling .items(), so clearly this function returns something very specific (dict_items)

I gave you the first step to help you unravel the mystery, and i like that you ask question, but being able to do research on your own, is also really important.

okay, what if you do:

d = {1:5, 2:2, 3:3}
for x in d.items():
    print(x[0], x[1])

see how we get all items? Like i said, i gave you one step, that shouldn’t stop you from analyzing further

You’ll often see a program step through pairs in a dictionary as

for key, value in my_dictionary.items():
     ...

In each pass through this loop, .items() is returning a value of data type ‘tuple’, which we unpack into two variables at a time, ‘key’ and ‘value’.
stetim94 has gone the extra mile to retain the original tuple as ‘x’ and instead index each of its two elements (x[0] and x[1]). Different syntax, same result.