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.
1 Like

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:

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

4 Likes

@mtf is this correct?

def sum_even_keys(my_dictionary):


  s=0

  for key in my_dictionary.items():

    if key[0]%2==0:

      s+=key[1]

  return s

For this line of code:

def sum_even_keys(my_dictionary):
total = 0
for key in my_dictionary.keys():
if key%2 == 0:
total += my_dictionary[key]
return total

Why does total += my_dictionary[key] because wouldn’t that just be the value of that key not the actual key even though we are trying to add key to total?

Looks about right :slight_smile: Do you get the output you expect? You can add function calls to test

You are currently adding the value to the total. Which is the task, right? Could you provide the exercise url?

Can someone explain why the second print function gives us 6 as the output.

Write your sum_even_keys function here:

def sum_even_keys(my_dictionary):

  counter = 0

  for key in my_dictionary.keys():

    if key % 2 == 0:

      counter += my_dictionary[key]

  return counter

# 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

what did you expect then?

the task is to sum the values when the keys are even.

I don’t understand how it summed up to 6. I don’t understand how they got that value. Could you explain as this is the solution and I was looking for a explanation on how they got the results. Thanks