0

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

q:2

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'

8
  • @Shilpa: You are the OP. Commented Jul 18, 2010 at 8:12
  • 2
    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. Commented Jul 18, 2010 at 8:25
  • oh god...wat shud i do now...When I post a similar question,.,,everybody here start saying that u shud not post the similar question again...u can edit the previous one...but now you r saying something else...but anyways...I am sorry...I'll keep it in my mind....And the error is only "w" .....This W is coming bcoz the value of (1,1) is west..so only w is coming out... Commented Jul 18, 2010 at 8:33
  • 2
    @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. Commented Jul 18, 2010 at 8:44
  • I am sorry....I'll not repeat this mistake...I am posting the whole new question with lil code ... Commented Jul 18, 2010 at 8:47

2 Answers 2

2

Works for me:

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

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):
  File "<stdin>", line 1, in <module>
KeyError: (7, 8)

In this case you either have to handle the exception:

try:
    final[(7,8)]
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.

Sign up to request clarification or add additional context in comments.

5 Comments

print final[(4,5)] gives me west as well. There must be something missing from the OP's question.
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: 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.
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: You should create a new question and post more complete code. Your code pieces are to small and we have to guess a lot.
2

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)]
'west'

2 Comments

KeyError: (5, 3) This is wat i am getting
@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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.