3/18: Is there a better way?


#1

Below is my code (which works to solve this exercise), I just want to know if there is a better way I am meant to be doing it here?

    my_dict = {"FirstName": "Mikel", "Height": 6.15, "Learning": True}        
    i = 0
    for key in my_dict:
        print my_dict.keys()[i], my_dict.values()[i]
        i += 1

For this exercise we are meant to use the for in structure, however is there a way to use the 'key' in the print statement (and get rid of the use of i)?

e.g. Something like: print my_dict.keys()[key], my_dict.values()[key]

Cheers for any suggestions..


#2

You can also do this

my_dict = {"FirstName": "Mikel", "Height": 6.15, "Learning": True}        
for key in my_dict:
    print key, my_dict[key]

#3

Perfect, thanks - it seems so obvious now you've written it! :smile:

(Aside: It must be hilarious how roundabout us beginner coders try to solve problems sometimes!..)