As you do the recursion task, based on your function, you can mix your function with a list or generator expression.
For example, consider the following code:
>>> f=lambda x:next(i for i in x if i.startswith('a')) >>> >>> a=['df','sr','t','aaf','ar','trf'] >>> a.index(f(a)) 3
You can navigate as follows using enumerate :
>>> next(i for i,j in enumerate(a) if j.startswith('a')) 3
So, all of this is based on your function , that you can put your structure into an understanding of a list or expression of a generator, and then apply some changes to it and use python tools based on your needs (in this case we used enumerate ).
source share