# Finding index in nested list

Stack Overflow Asked on December 16, 2021

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?

It always bothers me when someone sticks a for loop in the middle of a recursive function! Here's a different way to go about this problem:

def find_item(list_input, item, index=0):
if list_input:

return [index]

return [index] + result

return find_item(tail, item, index + 1)

return list_input

list_input  = [0, 5, [6, 8, [7, 3, 1]], 9, 10]

print(find_item(list_input, 7))


This solution doesn't require an explicit third argument. And returns an empty list upon failure to find the item, rather than None. Note the use of the new walrus operator :=

            if result := find_item(head, item):


If that's a problem, instead do:

            result = find_item(head, item)
if result:


Answered by cdlane on December 16, 2021

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


Answered by arsho on December 16, 2021

## Related Questions

### How do I run one function after another one in React?

2  Asked on January 3, 2022 by d-park

### C++ Selection Sort not Sorting Some Values of an Array

1  Asked on January 3, 2022 by steffen-brown

### SIM serial information API level 29

1  Asked on January 3, 2022 by fturriaf

### AJAX to fetch, get same funcionality

1  Asked on January 3, 2022 by reinaldo-ep

### Using Sequelize CLI with the ESM package

1  Asked on January 3, 2022 by wdgelwix

### How to make the corners of my border image rounder in kivy

1  Asked on January 3, 2022

### pymongo.errors.OperationFailure: Authentication failed when deploying chatbot on Heroku

1  Asked on January 3, 2022

### Why is my psycopg2 not working with python3?

0  Asked on January 3, 2022 by nicole-ganung

### Problem with even indexes in Java program

6  Asked on January 1, 2022 by iothio

### calling String method from different class to main class

1  Asked on January 1, 2022 by chan-myae-tun

### Re-arranging a two-way table

2  Asked on January 1, 2022

### Creating column names from multiple lists using for loop

1  Asked on January 1, 2022

### How can I fix this syntax error when importing functions from another library

3  Asked on January 1, 2022 by david-weisskopf

### How to correctly organize Tensorflow model layers?

1  Asked on January 1, 2022

### Vertically center radio button – React and Bootstrap

2  Asked on January 1, 2022 by a2441918

### COUNT Multiple Columns using GROUP BY – SQL

3  Asked on January 1, 2022 by barnee

### Visual Studio 2019 shows an empty solution after creating a .NET Core project

1  Asked on January 1, 2022 by araf-hasan

### Is it possible to replace the NULL value of a database with the current year in php pdo?

2  Asked on January 1, 2022 by ela-urbain

### Generate first and last status in separate columns SQL

2  Asked on January 1, 2022 by aww