Output of the key | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Output of the key

How do I get the key out of the dictionary?

1st Jul 2017, 6:16 AM
Kreez
Kreez - avatar
11 Answers
+ 2
if you want the keys in your dictionary d then use d.keys() which will return you a list of keys
1st Jul 2017, 6:20 AM
Sandesh
Sandesh - avatar
+ 1
Here I am a fool. I did not guess. Thank you so much
1st Jul 2017, 6:37 AM
Kreez
Kreez - avatar
0
You can iterate over the dictionary. So something like: for key in some_dict: print(key)
1st Jul 2017, 6:20 AM
Don
Don - avatar
0
But if I want to output a particular key?
1st Jul 2017, 6:24 AM
Kreez
Kreez - avatar
0
some_dict[key]
1st Jul 2017, 6:25 AM
Don
Don - avatar
0
еxample i have that p={"a":"g","b":"k"} and i want key with "k"
1st Jul 2017, 6:26 AM
Kreez
Kreez - avatar
0
It'll still follow the same format. It'll look like this: p['b']
1st Jul 2017, 6:28 AM
Don
Don - avatar
0
You did not understand me. I want to output a key that contains a "k".
1st Jul 2017, 6:30 AM
Kreez
Kreez - avatar
0
"You want to output a key that contains k". Alright, we can iterate over the dictionary and see if any of the keys corresponds to "k" and if so we'll print just that key. for key in some_dict: if some_dict[key] == 'k': print(key) or a list comprehension [print(key) for key in some_dict if some_dict[key] == 'k']
1st Jul 2017, 6:35 AM
Don
Don - avatar
0
Haha, no problem.
1st Jul 2017, 6:38 AM
Don
Don - avatar
0
dictionary stores data in this format key: value and you were asking for a value linked to a particular key.
1st Jul 2017, 6:42 AM
Sandesh
Sandesh - avatar