# FAQ: Code Challenge: Dictionaries - Even Keys

This community-built FAQ covers the “Even Keys” exercise from the lesson “Code Challenge: Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Even Keys

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Can you explain the example?

2 posts were split to a new topic: Do I need to reference locations in the dictionary with brackets?

5 posts were split to a new topic: How to solve this challenge with list comprehension

2 posts were split to a new topic: Why don’t my code work?

2 posts were split to a new topic: Can you help fix my code?

In section 3, “Even Keys”, of 3rd Code Challenge in Dictionaries, for the 2nd print is expected to return a wrong value. Sum of even KEYS is 1110, not 6. It’s asking for sum of the Keys that are Even, not Values.

For instance, this is my code and it’s working correctly, but not accepted by the website.

``````def sum_even_keys(my_dictionary):
Sum=0
for i in my_dictionary.keys():
if i%2==0:
Sum+=i
return Sum

print(sum_even_keys({1:5, 2:2, 3:3}))
# should print 2
print(sum_even_keys({10:1, 100:2, 1000:3}))
# should print 6 <<<<< This is a wrong expectation. It should return 1110.
``````
2 Likes

The name of the function is misleading, but we can accept that it’s not the keys that are being added. It should be the values with even keys.

4 Likes

I got it. So, it’s asking to check if the KEY is even, and if so, then the VALUE of that KEY in my_dictionary should be added to sum, not numerical value of the KEY. The confusion was made between numerical value of the KEY itself and VALUE of the KEY in my_dictionary.
I changed my code as below and now it’s working.

``````def sum_even_keys(my_dictionary):
Sum=0
for i in my_dictionary.keys():
if i%2==0:
Sum+=my_dictionary.get(i)
return Sum
``````

Thanks for the hint.

4 Likes
``````How to write the same program using list comprehension??
``````

Hi,
I have written program using simple for and if loop syntax. Below is the program.

``````def sum_even_keys(my_dictionary):
sum = 0
for keys, values in my_dictionary.items():
if keys % 2 == 0:
sum += values
return sum
# Uncomment these function calls to test your  function:
print(sum_even_keys({1:5, 2:2, 3:3}))
# should print 2
print(sum_even_keys({10:1, 100:2, 1000:3}))
# should print 6
``````

I wanted to use list comprehension. But not getting desired output.

``````def sum_even_keys(my_dictionary):
sum1 = 0
sum = [
sum1 += values
if keys % 2 == 0
for keys, values in my_dictionary.items():
]

return sum

# Uncomment these function calls to test your  function:
print(sum_even_keys({1:5, 2:2, 3:3}))
# should print 2
print(sum_even_keys({10:1, 100:2, 1000:3}))
# should print 6
``````

Thanks.

Why would you want to use list comprehension? List comprehension will give you a new list, while in this case you need an integer as result

If you want a challenge, try solving this problem with `.reduce`

“Create a counter variable and start it at `0` . Loop through all of the elements of the keys of the dictionary by using `my_dictionary.keys()` . If the key is even (which you can check by using `key % 2 == 0` ), add the corresponding value to the counter.”

This wording feels extremely confusing, you want the integer value of the KEY, not the index, not the corresponding value when you suggested a counter starting at 0 I was super confused.

I think should could use an example of how these would be calculated and why

For instance:

“The sum of `sum_even_keys({1:2, 18:55, 35:234})` would be `55` as the second index, index 1’s key is `18`, which is an even number and its corresponding value is `55`. The sum of `sum_even_keys({10:2, 100:5, 100:8})` would be `15` as keys of all indexes are even numbers, whose values total `15` (2 + 5 +8)”

To me this is just a matter of too many confusing terms to understand the desired outcome without an example.

Dictionaries after Python 3.6 are ordered, meaning their insertion order is preserved, but technically they do not have an index. However, since 3.7 and later are ordered, their `keys()` object can be enumerated.

Consider,

``````>>> a = [65,34,87,24,12,17,45,96]
>>> b = 'one two three four five six seven eight'.split()
>>> d = dict(zip(a, b))
>>> e = list(enumerate(d.keys()))
>>> e
[(0, 65), (1, 34), (2, 87), (3, 24), (4, 12), (5, 17), (6, 45), (7, 96)]
>>>
``````

So now we can see the indices, though it’s not the same as keys having an index.

There is one problem with the above. Dictionaries do not have duplicate keys. If that was the order of insertion, then the sum would be 10, not 15 since the second 100 key-value will overwrite the value of the first.

As far as the instructions go, there is plenty of free information. We are told to set a counter to 0.

``````count = 0
``````

We’re told to iterate over the dictionary keys using the `dict.keys()` method.

``````for key in my_dictionary.keys():
``````

We’re told how to determine which keys are even…

``````    if key % 2 == 0:
count += my_dictionary[key]
``````

Too many times we are lectured by new learners who believe they know better how to write a lesson or exercise, and most of the time they are wrong. It’s hubris to assume the student is the better teacher. Blaming the narrative because you maybe jumped ahead of the lessons (or rushed along too quickly without review or practice) is a sure sign of entitlement. We don’t get paid to manage these forums, so shouldn’t have to put up with the gripes. If you have a problem with CC, then take it up with them and keep it off these boards.

3 Likes