JMz
JMz

Reputation: 71

python - Getting what's inside of a dictionary key

class graph:
    def __init__(self):
        self.vertex = {}

    def agregarVertex(self, vertex):
        self.vertex[vertex] = {}

    def getVertCoords(self, vertex):
        if vertex in self.vertex.keys():
            print self.vertex.get(vertex)

So I am trying to get the values that are in that key, when I run the program I get

{<Vertex.vertex instance at 0x01DF5120>: 1}

So that shows me inside there is a Vertex, how can I access the values of said vertex?

Upvotes: 0

Views: 1043

Answers (1)

tknickman
tknickman

Reputation: 4621

You can access data in a dictionary key just like any other python data type (keep in mind only hashable datatypes can be used as dict keys). Check out the examples below (I'm doing key.method() since your example looks like your keys are objects)

for key in your_dict:
   value = your_dict[value]
   key.method()

Or

for key, value in your_dict.iteritems():
    # do stuff with the key
    key.method()

Also if you just want the keys of the dictionary:

all_keys = your_dict.keys()
for key in all_keys:
    key.method()

Upvotes: 1

Related Questions