Retrieve value from key : Index « Dictionary « Python Tutorial






validkeys = (1,2,3)
keyGenDict={'keys':[1,2,3],1:'blue',2:'fast',3:'test','key':2}

def show_key (key):
    if(key in validkeys):
        keyVal = (keyGenDict["keys"])[key-1]
        print "Key = " + keyGenDict[keyVal]
    else:
        print("Invalid key")

val = keyGenDict["key"]
show_key(val)








8.12.Index
8.12.1.Indexing a Dictionary
8.12.2.Retrieve value from key
8.12.3.Modifying a Dictionary
8.12.4.How to Access Values in Dictionaries
8.12.5.To access individual dictionary elements, you use the familiar square brackets along with the key to obtain its value:
8.12.6.mixing the use of numbers and strings as keys
8.12.7.Dictionary methods.