Loops 14 - Looping over a dictionary - different answer?


#1

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)


#2

Hi @rebel62

Running:

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

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

returns:

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

as expected for me

What kind of error message are you getting?


#3

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.


#4

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

dict.keys()

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

and dict.values()

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
else:
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
    else:
      print "Odelay!"
    return sum

#5

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


#6

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"
print(d.keys())
print(d.values())

will yield the same result

:smile:


#7

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


#8

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.


#9

You're 100% percent correct.


#10

This works for me

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

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

#12

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
None

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


#13

It does work. Thanks


#14

@cosminmarginean

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'}
print(OrderedDict(sorted(a.items())))

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


#15

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


#16

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


#17

On point there. Just that simple


#18

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


#19

yes very good i was getting a bit confused


#23