Learn Algorithm Design by Building a Shortest Path Algorithm - Step 29

Tell us what’s happening:

I get the error that I should have a paths variable- I do.

I believe my error is with what I put inside the variable. But not exactly sure as I don’t get an error regarding that.

Your code so far

my_graph = {
    'A': [('B', 3), ('D', 1)],
    'B': [('A', 3), ('C', 4)],
    'C': [('B', 4), ('D', 7)],
    'D': [('A', 1), ('C', 7)]

/* User Editable Region */

def shortest_path(graph, start):
    unvisited = list(graph)
    distances = {}
    paths = {key:[] in graph.items}
    print(f'Unvisited: {unvisited}\nDistances: {distances}')
shortest_path(my_graph, 'A')

/* User Editable Region */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Learn Algorithm Design by Building a Shortest Path Algorithm - Step 29

1 Like

On the right track but you are definitely missing important items in your dict comprehension. Remember doing list comprehensions, it’s similar. You need “FOR” and “IN” there, just like a for loop. This is the essential structure of a comprehension. Check the example again:

{key: val **for** key **in** dict}

This will copy all the keys and values from dict. All you want is the keys, instead of the val you just want an empty list.

paths = {key:[]

You got it, looks good. Back to the example:

for key in dict}

Copy that, but your dictionary is called graph

in graph.items}

Almost good! Why did you decide to add items to the end?


I added graph items because I overcomplicated the instructions (assign EVERY item from graph).

Your explanation was so clear and helpful, thanks so much :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.