There is only 1 method you need to try, slice: my_duplicate_list = my_list[:]
You are copying the list, by creating a new list, by using a slice of the list from beginning to end. You just need to add [:] to the end of paths[current]
This is a normal assignment, but the problem is that these still point to the same list in memory. To make a real copy, create a new list by making a full slice:
my_duplicate_list = my_list[:]
This creates a new list and not just a new pointer.
There was slicing in previous project within the lesson, however the current project does not clearly explain the need to slice.
Step 47 Instructions
The other bug is subtle. When a shorter distance is found for a neighbor node, paths[current] gets assigned to the neighbor node path, paths[node].
This means both variables point to the same list. Since lists are mutable, when you append the neighbor node to its path, both paths[node] and paths[current] are modified because they are the same list. This results in wrong paths, although the distances are correct.
Fix that bug by assigning a copy of paths[current] to the neighbor node path. Modify the existing assignment inside your if block.
# This was default
if paths[node] and paths[node][-1] == node:
paths[node] = paths[current]
# However, this code does not pass
if paths[node] and paths[node][-1] == node:
paths[node] = paths[current].copy()
Error Instructions:
Sorry, your code does not pass. Keep trying.
You should use the slice syntax to assign a copy of paths[current] to the neighbor node path.
My Takeaway:
—> Yes , the error instruction is to slice whole list. However, it was never been explained before that this will make a copy when we are building a project about list manipulation
→ Also, I have just noticed that the search for help prompt will never display any answered result.