How to get a certain value of a certain key in dictionary?

linaki865 :
G={(-1, 1): [(0, 1)],
 (0, 0): [(1, 0), (0, 1)],
 (0, 1): [(1, 1), (0, 2), (-1, 1), (0, 0)],
 (0, 2): [(0, 1)],
 (1, 0): [(2, 0), (1, 1), (0, 0)],
 (1, 1): [(0, 1), (1, 0)],
 (2, 0): [(1, 0)]}

I have this dictionary in python and i want to go through each value of a certain key. For example i have the key: (0,0) I want to print each value seperately like this:

Output:

value 0 : (1,0)
value 1 : (0,1)

and I have this code

key=(0,0)
for v in range (len(G[key])):
    print ("value ", v, ":", G[(0,0)].get(v))

G[(0,0)].get(v) is wrong and i get a message:

AttributeError: 'list' object has no attribute 'get'

Do you know what should i use instead?

quamrana :

enumerate will get you the index and value:

key=(0,0)
for idx,v in enumerate(G[key]):
    print ("value ", idx, ":", v)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=319246&siteId=1