FAQ: Graphs: Python - Building the Vertex II

This community-built FAQ covers the “Building the Vertex II” 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 Vertex II

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!

Why in the practice, it is add a ‘value’ instead of a vertex?
I thought add_edge method are supported to connect each vertex, so the argument should be a vertex. So I wrote

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

And the example showed in the instruction grand_central.add_edge(forty_second_street) also add a vertex instead of a value. so why in the practice, it become add a vertex’s value?
Could anyone explain this to me?


I know that this comes a months later, but since i just stumbled with this lesson i thought it would be worth to reply:

In the practice, we are adding the value of another vertex, maybe for readability purposes. You can add the vertex object but when you print you won’t get the output in string format (need to rely to vertex.value.

self.edges[vertex.value] = True
This will work if you add a vertex and not the value, but you have to put in the print statement
print('Adding edge to {0}'.format(vertex.value))
In both cases we will add the value of the vertex.

There is no difference between what you are proposing and what the lesson explains.


adding the vertex itself is a viable solution to map the edges.

however when accessing the edges later it will return <vertex object at 432958423xo4> instead of the name of the vertex that is connected.

i believe adding “.value” to the argument every time you call “.add_edge()” is cumbersome and dont like codecademys solution. Here is what i believe to be a more elegant approach:

def add_edge(self, vertex):

print("Adding edge to " + str(vertex.value)) #prints the vertex's value being added

self.edges[vertex.value] = True #adds the vertex value to our dictionary of edges

This task made me realise that I don’t understand how the init constructor works.

What is self.value = value doing?
Is it saying that if I run it on an object, then a .value attribute is assigned to this object and the object’s “value attribute” will now be the same as the “value”?

If that is the case, then when:


is called, is it the same as writing:

grand_central.add_edge("42nd Street Station")

because both refer to the “42nd Street Station” vertex?

This is the code I use for the task:

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

  # define .add_edge() here

  def add_edge(self, vertex):
    print("Adding edge to " + vertex)
    self.edges[vertex] = True

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

grand_central = Vertex('Grand Central Station')
forty_second_street = Vertex('42nd Street Station')


# call .add_edge() below here



Couldn’t you just create a repr method for the vertex so it prints whatever you want it to print?

I believe using the vertex, and not the value, makes the method way more consistent.

What does

self.edges[vertex] = True

do? I never seen this before.

In the Vertex class self.edges is created as a dictionary, self.edges = {}. Using self.edges[vertex] = True creates a new key-value pair where the argument passed to vertex is the key and True is the value.

# Similar dictionary usage-
test_dict = {"a" : 0}
test_dict["b"] = False  # this just adds a new key-value pair to test_dict
Out: {'a': 0, 'b': False}

In the given example you can check what’s stored as follows-

Out: {'42nd Street Station': True}