Skip to content
Advertisement

Finding index in nested list

I am trying to create a function that will take as input a nested list and an item, and return a list of indices. For example list = [0, 5, [6, 8, [7, 3, 6]], 9, 10] and item = 7 should return [2, 2, 0], since list[2][2][0] = 7

my code should work since I can print the desires output, but when i run it it returns None.

JavaScript

what am I overlooking here?

Advertisement

Answer

As @tzaman mentioned, you need to handle the return value of find_item recursive call. If the return value of the recursive call is a list, it will mean that searched item is found and we need to stop the recursion.

The following modification will return the earliest found index of the searched item. If no item is found, it will return None.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement