Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Dictionary get value of Key [duplicate]

Is there a way to get the value of the key, not the key value in a dictionary? for example:

d = {"testkey": "testvalue"}
print(d.get("testkey"))
#OUTPUT: "testvalue"

Is there a way to get the String "testkey"? I will have no way of knowing what the String returned will be in the end. Would it be more beneficial to use a list instead of a dictionary?

like image 955
Aaron Avatar asked Feb 09 '23 08:02

Aaron


1 Answers

You are looking for the keys() function (used as d.keys()).
You may also use this:

for key in d:
   print "key: %s , value: %s" % (key, d[key])

for all the information.

like image 178
Idos Avatar answered Feb 12 '23 12:02

Idos