I have a nested list, like this one for example:
test = [[15, [7, [None], [11, [None], [13, [None], [None]]]], [None]], [20, [None], [None]]]
I was wanting to create another list from this with only integers contained in the nest. Which would return this:
[15, 7, 11, 13, 20]
I have made this recursive function to do what I needed to accomplish but, I couldn't help to think this isn't the best way to go about it. Is there a more pythonic or efficient way to do it?
def nest_search(nest, hold=[]):
for item in nest:
if isinstance(item, int):
hold.append(item)
if isinstance(item, list):
nest_search(item, hold)
return hold
>>> print nest_search(test)
[15, 7, 11, 13, 20]