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.
def find_item(list_input, item, current): if list_input == item: print(current) ## this does give the desires output, but the function should return not print return current else: if isinstance(list_input, list): for j in range(len(list_input)): current.append(j) find_item(list_input[j], item, current) del current[-1]
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
.
def find_item(list_input, item, current): if list_input == item: return current else: if isinstance(list_input, list): for j in range(len(list_input)): current.append(j) search_result = find_item(list_input[j], item, current) if isinstance(search_result, list): return search_result del current[-1] list_input = [0, 5, [6, 8, [7, 3, 6]], 9, 10] item = 7 print(find_item(list_input, item, [])) list_input = [0, 5, [6, 8, [7, 3, 6]], 9, 10] item = 9 print(find_item(list_input, item, [])) list_input = [0, 5, [6, 8, [7, 3, 6]], [30, 4], 9, 10] item = 4 print(find_item(list_input, item, [])) list_input = [0, 5, [6, 8, [7, 3, 6]], [30, 4], 9, 10] item = 400 print(find_item(list_input, item, []))
Output:
[2, 2, 0] [3] [3, 1] None