0
Output of the key
How do I get the key out of the dictionary?
11 Réponses
+ 2
if you want the keys in your dictionary d then use d.keys() which will return you a list of keys
+ 1
Here I am a fool. I did not guess. Thank you so much
0
You can iterate over the dictionary. So something like:
for key in some_dict:
print(key)
0
But if I want to output a particular key?
0
some_dict[key]
0
еxample i have that
p={"a":"g","b":"k"}
and i want key with "k"
0
It'll still follow the same format. It'll look like this:
p['b']
0
You did not understand me. I want to output a key that contains a "k".
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']
0
Haha, no problem.
0
dictionary stores data in this format key: value and you were asking for a value linked to a particular key.