Reputation: 61
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?
Upvotes: 2
Views: 60
Reputation: 17322
you can use print
build-in function with param sep='\n'
:
print(*G[(0,0)], sep='\n')
output:
(1, 0)
(0, 1)
or you can use:
print(*['value %s : %s' % t for t in enumerate(G[(0,0)])], sep='\n')
output:
value 0 : (1, 0)
value 1 : (0, 1)
Upvotes: 2
Reputation: 39422
enumerate will get you the index and value:
key=(0,0)
for idx,v in enumerate(G[key]):
print ("value ", idx, ":", v)
Upvotes: 4