Loops 14 - Looping over a dictionary - different answer?


Here is the answer provided by 'Stack Overflow'.

print d.keys(), d.values()

Why does it not work here?

(I've got the required answer already but I'm still wondering)


Hi @rebel62


d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

print d.keys(), d.values()


['a', 'c', 'b'] ['apple', 'cherry', 'berry']

as expected for me

What kind of error message are you getting?


Prob same but the consoles here seem to give dodgy unhelpful error messages. Are you familiar with the above notation?

I like Stack Overflow and usually rely on them to take up the slack when the course writers here are inadequate.


For the few problems I've encountered during this course I've prefered to turn towards the official Python Documentation for answers. :smile:

Here's what it has to say about


Return a copy of the dictionary’s list of keys. See the note for dict.items().

and dict.values()

Return a copy of the dictionary’s list of values. See the note for dict.items().

Both functions being relatively simple to use, it makes me very curious about the problem you are experiencing

Could you paste the exact and entirety of the code you are trying to run and the error message you are getting and a link to the page you are having the problem so we can try to help you understand what could be happening?

This forum supports Markdown you can use three backticks before and after your code block like so:

def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
print "Odelay!"
return sum

and it will retain proper indentation and even have nifty syntax highlighting:

def my_func(x):
    my_var = 0
    if x > my_var:
      sum = my_var + x
      print "Odelay!"
    return sum


print (d.keys(), d.values())
Need parentheses for Python 3.x...


True @olto_oli

I think the course interpreter is Python 2.7.2 but as far as print goes, it seems to understand the syntax of both v2 and v3:

d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}
print "Python 2 style"
print d.keys()
print d.values()
print "Python 3 style"

will yield the same result



Oh okay,
So I gather it would have worked with extra parenthesis and that it was prob v. 3 Python in Stack overflow.

Thank you for that info. Cheers


d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:

print key, d[key]

I think everyone is over complicating this. You are already looping through dictionary d with respect to key. You just need to print the key, and the value associated with that key.


You're 100% percent correct.


This works for me

d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
    # Your code here!
    print key, d[key]


I have used this:

d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
# Your code here!
print key, d[key]

and I get this:

a apple
c cherry
b berry

Anyone able to explain why the return result is not chronological ordered?


It does work. Thanks



Dicts are not stored like that, they use a hash table. If you want them to be ordered you have to do something like

from collections import OrderedDict
a = {'a':'b', 'c':'d', 'e':'f'}

This will sort your dict like you would expect of a list.


What confuses me here is why d[key] would print out the value and not the key again :\


dictionary = { 'key' : 'value' }
print [key for key, item in dictionary.items()]
# ['key']
print dictionary['key']
# value

Notice that the value is associated with the dictionary subscript (the key).


On point there. Just that simple


I dont get it where exactly do we associated the value with the key, yea probably in the "for" but i cant explain it to myself


yes very good i was getting a bit confused