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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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.

3 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 :slight_smile: