educative.io

Why is currentpath variable is type casted to list when it is already a list

Why is currentpath variable type casted

2 Likes

At least in Python, the code is not type casting the currentPath variable instead, it’s passing the list referenced by currentPath and passing it into the list object constructor to create a new list object.

Here the list() is not for the purpose of typecasting, it is to create a copy of the current list. If you don’t do that, every time you append to allPaths, it will be pointing to the same reference. So instead of appending multiple lists to allPaths, you would end up having one big list with all elements from all the function calls.

2 Likes