Take the 2-minute tour ×

I made a dictionary, then split up the values and keys into lists and now its looks like this:

keys = [(4,5),(5,6),(4,8)......so on].
values = [('west',1),('south',1).......]

Then I made a new dictionary like in this way,

final = dict((k,v[0]) for k,v in zip(keys, values))

When I execute -print final - output is in this form... {(4,5):west,(5,6):south,......so on}

Now i need to have the value of key (4,5)...it can be any key..


win = gap.pop() - here gap is a stack
         print win      - the output is (1,1)
         return final.get(win) -

but when I do this return, it gives me an error and final is the directory that I have made with lists of keys and values

The error is: 'W'

share|improve this question
what is OP here? –  Shilpa Jul 18 '10 at 8:09
@Shilpa: You are the OP. –  Felix Kling Jul 18 '10 at 8:12
OP == original poster –  qbi Jul 18 '10 at 8:14
Shilpa, instead of editing and adding a new question, just start a new question. Also, it's probably more helpful to copy/paste code and error messages. Things get left out otherwise and it's hard to guess. For example. the error message "W" -- there has to be be more than just that. –  ars Jul 18 '10 at 8:25
@Shilpa: I see you ask a lot about python and every question gives you a new piece for your application. Seriously, this won't help you. Learn Python before you use it. SO is not for learning a language, it is for helping with specific problems. But it seems you gain the most if you first read a Python tutorial and follow some code examples. –  Felix Kling Jul 18 '10 at 8:44
show 6 more comments

2 Answers

up vote 2 down vote accepted

Works for me:

>>> final = {(4,5):"West", (5,6): "East"}
>>> print final
{(4, 5): 'West', (5, 6): 'East'}
>>> final[(4,5)]

You might want to try final.get((4,5)).

Or post more code, maybe you do something fancy with final. If you don't get a value back, you should at least get a KeyError:

>>> final[(7,8)]
Traceback (most recent call last):
KeyError: (7, 8)

In this case you either have to handle the exception:

except KeyError:
    print "Key not in dict."

or use final.get((7,8), <default value>) which will return <default value> if the key is not found (or None if you don't specify a default value).

Read about dictionaries in the Python documentation.

share|improve this answer
print final[(4,5)] gives me west as well. There must be something missing from the OP's question. –  Johnsyweb Jul 18 '10 at 8:10
Awesome....final.get((4,5)) works for me...thanks But i have 1 more question related to this thing...I am editing the question...plz see it and tell me the right way of doing it...once again..thanks –  Shilpa Jul 18 '10 at 8:14
@Shilpa: You're welcome, but it seems you have not stated your problem correctly. First you said, final[(4,5)] does not give you west but we could show you that it does. Later in a comment you say that you get a key error for (5,3), but you never mentioned this in your question. Please be specific about your problem, we don't want to guess. –  Felix Kling Jul 18 '10 at 8:22
Actually the problem is that I am not getting the result for any number. Only the final.get gives me the result. I dont see, weher I used (5,3)..bt leave it now, its not an issue....I need to find the soln of my next error.....why is that so ?/ –  Shilpa Jul 18 '10 at 8:37
@Shilpa: You should create a new question and post more complete code. Your code pieces are to small and we have to guess a lot. –  Felix Kling Jul 18 '10 at 8:40
add comment

Works for me:

>>> keys=[(4,5),(5,6)]
>>> values = ["west","south"]
>>> f=dict(zip(keys,values))
>>> f
{(4, 5): 'west', (5, 6): 'south'}
>>> f[(4,5)]
share|improve this answer
KeyError: (5, 3) This is wat i am getting –  Shilpa Jul 18 '10 at 8:07
@Shilpa: Well, then the key (5,3) is obviously not in your dictionary. From the code you posted so far it seems you have (a,b), where a < b, but 5 is larger than 3. If you print final, do you see a key (5,3)? I bet not ;) –  Felix Kling Jul 18 '10 at 8:08
yes,it has....but now i solved the problem..thanks –  Shilpa Jul 18 '10 at 8:24
add comment

Your Answer


