how to have two different keys for the same value in a dictionary in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to have two different keys for the same value in a dictionary in python

I was just trying to program a code to get the position of the alphabet wordval = { "a" or "A": 1, } print(wordval["A"]) but when i try it myself then there comes an error Pls. guide me how tackle this problem

11th Aug 2020, 2:33 AM
Smiley[Offline]
6 Answers
+ 3
This is not a direct answer to your question, but since you only want the value you don't need a dict at all, you can do it like this: def pos(x): if ord(x)>ord('Z'): return ord(x)-ord('a')+1 return ord(x)-ord('A')+1
11th Aug 2020, 3:00 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
smiley the code I wrote doesn't give you the ASCII value, it gives you the exact result as your dictionnary. pos('a') and pos('A') will return 1, pos('b') and pos('B') will return 2, and so on.
11th Aug 2020, 3:41 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
just assign both to the same value: dict = { "a": 1, "A": 1, } If this doesn't work you could also use the string.lower() method when trying to access the dictionary: dict = { "a": 1 } print(dict["A".lower()])
11th Aug 2020, 2:37 AM
Brian R
Brian R - avatar
+ 1
Aymane Boukrouh thanks sir
11th Aug 2020, 3:50 AM
Smiley[Offline]
0
Aymane Boukrouh sir I don't want the ASCII value of the alphabet but wanted to print the place value of the alphabet Like I did here https://code.sololearn.com/cXdJ3uL74z0X/?ref=app Please help me !!
11th Aug 2020, 3:39 AM
Smiley[Offline]
11th Aug 2020, 9:26 AM
Steven M
Steven M - avatar