How to retieve value from a dictionary based on key ?
up vote
-1
down vote
favorite
I am stuck at this minor problem for quite some time and cannot understand why.
lets say I have a list :
test = ['1', '2', '3']
I convert this into dictionary using
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
Now when I access key based on value like this
print (a.get('1'))
it gives me None. Any suggestions in this regard would be helpful.
python
add a comment |
up vote
-1
down vote
favorite
I am stuck at this minor problem for quite some time and cannot understand why.
lets say I have a list :
test = ['1', '2', '3']
I convert this into dictionary using
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
Now when I access key based on value like this
print (a.get('1'))
it gives me None. Any suggestions in this regard would be helpful.
python
Tryprint (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am stuck at this minor problem for quite some time and cannot understand why.
lets say I have a list :
test = ['1', '2', '3']
I convert this into dictionary using
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
Now when I access key based on value like this
print (a.get('1'))
it gives me None. Any suggestions in this regard would be helpful.
python
I am stuck at this minor problem for quite some time and cannot understand why.
lets say I have a list :
test = ['1', '2', '3']
I convert this into dictionary using
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
Now when I access key based on value like this
print (a.get('1'))
it gives me None. Any suggestions in this regard would be helpful.
python
python
asked Nov 11 at 15:09
Muss
296
296
Tryprint (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15
add a comment |
Tryprint (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15
Try
print (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Try
print (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Its a problem of type
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
The keys will be of integer type and not string type.
Try this out instead
print (a.get(1))
Edit:
To get the keys, you can flip the dictionary creation
test_dict = { test[i] : i for i in range(0, len(test))}
print (a.get(1))
0
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Its a problem of type
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
The keys will be of integer type and not string type.
Try this out instead
print (a.get(1))
Edit:
To get the keys, you can flip the dictionary creation
test_dict = { test[i] : i for i in range(0, len(test))}
print (a.get(1))
0
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
add a comment |
up vote
1
down vote
Its a problem of type
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
The keys will be of integer type and not string type.
Try this out instead
print (a.get(1))
Edit:
To get the keys, you can flip the dictionary creation
test_dict = { test[i] : i for i in range(0, len(test))}
print (a.get(1))
0
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
add a comment |
up vote
1
down vote
up vote
1
down vote
Its a problem of type
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
The keys will be of integer type and not string type.
Try this out instead
print (a.get(1))
Edit:
To get the keys, you can flip the dictionary creation
test_dict = { test[i] : i for i in range(0, len(test))}
print (a.get(1))
0
Its a problem of type
test_dict = { i : test[i] for i in range(0, len(test))}
{0: '1', 1: '2', 2: '3'}
The keys will be of integer type and not string type.
Try this out instead
print (a.get(1))
Edit:
To get the keys, you can flip the dictionary creation
test_dict = { test[i] : i for i in range(0, len(test))}
print (a.get(1))
0
edited Nov 11 at 15:24
answered Nov 11 at 15:14
Sharath
114
114
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
add a comment |
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
It gives the value of 1. I need to get key instead like if I do test_dict[1] it should give me 0
– Muss
Nov 11 at 15:20
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
Flip the dictionary creation around then. Check the answer I have edited it.
– Sharath
Nov 11 at 15:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250065%2fhow-to-retieve-value-from-a-dictionary-based-on-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
print (test_dict[1]) #=> 2
– iGian
Nov 11 at 15:12
Dictionaries are a unidirectional mapping of keys to values.
– timgeb
Nov 11 at 15:14
I want to print the key not value. This would give 2 which the is the value of 1. I want to do it the other way round
– Muss
Nov 11 at 15:15