# 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 for x in d.items() if x % 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 1 Like

ah, `x` and `x` 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 is the first key and x is the first value? In that case x will be second key, x 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 and x) and just return x (a value from first pair). x and x 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, x)
``````

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 and x). Different syntax, same result.