1

I have a dict whose keys are numbers. It looks like this:

{'1': 3, '2': 7, '3', 14}

I need to access the value of each key with a variable, but get stuck because of the quotation marks of keys. How can I do that?

6
  • 1
    Can you show your code please? Commented Sep 6, 2016 at 3:30
  • The quotation marks mean they're strings. Commented Sep 6, 2016 at 3:30
  • We can help you better if we see your code. Commented Sep 6, 2016 at 3:31
  • Welcome to Stack Overflow! Unfortunately it's a little hard to tell what you're asking. Could you provide what you've tried so far, what the error is, and the result you want? Commented Sep 6, 2016 at 3:31
  • typo: {'1': 3, '2': 7, '3': 14} Commented Sep 6, 2016 at 3:35

1 Answer 1

2

You can cast your number to a string,

my_dict = {'1': 3, '2': 7, '3': 14}

number = 2
print(my_dict[str(number)])
Sign up to request clarification or add additional context in comments.

Comments

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.