Python nested list recursion search
list, python, recursion
Solution
You can squeeze your entire code like this
def search(current_item, number):
if current_item == number: return True
return isinstance(current_item, list) \
and any(search(item, number) for item in current_item)
You can test it like this
for i in range(12):
print i, search([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], i)
Output
0 False
1 True
2 True
3 True
4 True
5 True
6 True
7 False
8 True
9 True
10 True
11 False
The logic is like this,
If the `current_item` is equal to the number we are looking for, return `True`.
If the `current_item` is not an instance of list, then it is a number in this case. If it is a number and it does not equal to `number` we should return `False`.
If the `current_item` is a list, then go through each and every element of it and check if any of the elements has `number`. `any` returns immediately after getting the first `True` value.
Problem
Given a nested list L (such that each element of L is either an integer, or a list, which may itself contain integers, or lists, which may in turn.... etc) return True i s is in L. ``` search([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8) ``` should return True. Here is what I have so far: ``` def search (L,s): """(list, anytype) _> Bool Returns true iff s is present in L """ if L: if L[0] == s: return True else: return search (L[1:], s) else: return False ``` This current code works for a list if it isn't nested, or if it is nested like so (the nested element is the last element): ``` [1, 2, 3, [4, 5]] ``` But not for the following: ``` [1, 2, [3, 4], 5] ``` How can I change my code so that it works for a nested list? with the nested element being anywhere in the list not strictly the last element? Appreciate any help! EDIT: Sorry, forgot to specify that it needs to be recursive.