Keys() and Values()


<As you can see from my code, I have skirted round the internet to help me out with this exercise but no joy I seem to get the coding correctly but I fail to achieve what the exercise requires. Please help. I always get the message "Oops, try again. It looks like your code still contains a call to my_dict.items()" with whichever code I submit. Nobody else seem to have the same problem so I must be missing something basic. I get the result
as expected(??)
Thank you .

my_dict ={ "John": "Lewis", "Gordon": "James", "Honky": "Dorry"}
for value in my_dict.itervalues():
print value
for key in my_dict.iterkeys():
print key



my_dict = {
  "John": "Lewis",
  "Gordon": "James", 
  "Honky": "Dory"

Your data structure is a dict class with both values() and keys() methods. Use the method asked for in the lesson.

for value in my_dict.values():
    print value

for key in my_dict.keys():
    print key


Thank you Roy. I still get the same message "Oops, try again. It looks like your code still contains a call to my_dict.items() . "


Can you link us to the lesson, please? Thanks.

Did your code from a previous lesson carry over to this? I can't recall if items was covered first, or later. The items method returns a list of tuples so that output will look strange to the lesson checker if it is present when not expected.

>>> my_dict = {'a': 1, 'b': 2, 'c': 3}
>>> my_dict.items()
dict_items([('b', 2), ('a', 1), ('c', 3)])
>>> for x in my_dict.items():
	print (x)

('b', 2)
('a', 1)
('c', 3)


Yes of course Roy. The code for the first lesson is as shown below and the print out was/is
[('Honky', 'Dorry'), ('John', 'Lewis'), ('Gordon', 'James')]
my_dict ={ "John": "Lewis", "Gordon": "James", "Honky": "Dorry"}
print my_dict.items()
The link for keys() and values() is

I hope that helps. Thank you


Perhaps if your try the direct approach, without any loops.

print my_dict.values()
print my_dict.keys()

Be sure to delete your earlier method call.


My interpretation of the requirement of the exercise is to "list" the key as an array and similarly the values but without using my_dict. I may be wrong.


The above two direct print statements let me pass the lesson.


Hiya Roy, I think that may work in part but, on the console, it gives me


but gives me another

"Oops, try again. It looks like your code still contains a call to my_dict.items() ".


Please post your code. Thanks.


Oops sorry. Please find code below. Cheers

my_dict ={ "John": "Lewis", "Gordon": "James", "Honky": "Dorry"}
print my_dict.values
print my_dict.keys


Recall that both methods require arguments. (), even if empty.


that worked as in

['Dorry', 'Lewis', 'James']
['Honky', 'John', 'Gordon']
but I still get "Oops, try again. It looks like your code still contains a call to my_dict.items() " I appreciate your patience.

my_dict ={ "John": "Lewis", "Gordon": "James", "Honky": "Dorry"}
print my_dict.values()
print my_dict.keys()


Did you manage to sort this out, yet?


Hiya , Sorry for the delay. I have been away.
No I have not been able to sort out this problem.It seems that everybody else, except me, has solved it. I am running out of options and I cannot proceed until this exercise is solved. Hmm
I am recalling/remembering quite a few things from trying your suggestions. For instance, the value and keys are returned as two index matching) lists. (Python is the first coding language I have tried}.


Having copied the code, in frustration, I pressed the "reset code" button to start all over again. I then entered the same code and it worked.
Thank you


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.