Keys() and Values()


#1


<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
Dorry
Lewis
James
Honky
John
Gordon
None
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

`


#2

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

#4

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


#5

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)
>>>

#6

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')]
None
my_dict ={ "John": "Lewis", "Gordon": "James", "Honky": "Dorry"}
print my_dict.items()
The link for keys() and values() is
https://www.codecademy.com/en/courses/python-beginner-en-KAgt5/0/2?curriculum_id=4f89dab3d788890003000096.

I hope that helps. Thank you


#7

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.


#8

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.


#9

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


#10

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



None

but gives me another

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


#11

Please post your code. Thanks.


#12

Oops sorry. Please find code below. Cheers

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


#13

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


#14

that worked as in

['Dorry', 'Lewis', 'James']
['Honky', 'John', 'Gordon']
None
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()


#15

Did you manage to sort this out, yet?


#16

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


#17

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


#18

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