FAQ: Graphs: Python - Building the Graph I

This community-built FAQ covers the “Building the Graph I” exercise from the lesson “Graphs: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Building the Graph I

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi,

After completion of task #4, my code is returning:

{‘Grand Central Station’: <vertex.Vertex object at 0x7feef4ea6710>}

instead of:

{“Grand Central Station”: grand_central}

Any idea how to display the variable name as the value? Thanks!

4 Likes

I highly suspect that it’s because our variable is a Vertex class which doesn’t have a string method?
.
I think the best way to get that result is by declaring __repr__ method inside Vertex class.
.
But I still don’t know how to convert the variable name into string as well.
hope someone can explain it better.

I managed to print the variable name to the list. the way I did it was to copy the Vertex.py code to the graph.py code and add the following repr to the vertex class.

  def __repr__(self):
      d = [k for k,v in globals().items() if v is self]    
      return str(d[0])

maybe you can do it somehow differently, but i don’t know how to debug that properly. Would love to know tho! The class is probably not part of globals because it’s in a different file. Therefore, adding the code above to vertex.py would return an index error as the variable d is empty.
here is the complete code:

class Vertex:
  def __init__(self, value):
    
    self.value = value
    self.edges = {}

  def add_edge(self, vertex):
    self.edges[vertex] = True

  def get_edges(self):
    return list(self.edges.keys())

  def __repr__(self):
      d = [k for k,v in globals().items() if v is self]    
      return str(d[0])

class Graph:
  def __init__(self,directed = False):
    self.directed = directed
    self.graph_dict = {}
  
  def add_vertex(self,vertex):
    print("Adding "+ vertex.value)
    self.graph_dict[vertex.value] = vertex



grand_central = Vertex("Grand Central Station")

# Uncomment this code after you've defined Graph
railway = Graph()

# Uncomment these lines after you've completed .add_vertex()
print(railway.graph_dict)
railway.add_vertex(grand_central)
print(railway.graph_dict)
1 Like

I managed to get the string value by adding :

def __repr__(self):
  return self.value

in vertex.py

4 Likes

Thank you for this <3