Why does 3 in [[1, 2, 3]] return False, and how does recursion flatten and search nested lists whose depth you can't know in advance?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The in operator only ever looks at the top-level elements of the list you hand it.
Two lists are given: flat = [10, 20, 30] and nested = [[10, 20, 30]]. Assign to membership a list of exactly three booleans, in this order:
20 is an element of flat20 is an element of nested20 is an element of the single inner list that nested holdsWrite each entry as a test that Python evaluates for you — do not type True / False out by hand.
flatten as concatenation of sublists
Write return L[0] + flatten(L[1:]) with the len(L) == 1 case returning L[0], and run it on [[1, 2], [3, 4], [9, 8, 7]] for [1, 2, 3, 4, 9, 8, 7].
Searching inside sublists against top-level membership
Record that 3 in [1, 2, 3] is True while 3 in [[1, 2, 3]] is False, then write in_lists_of_list testing e in L[0] and recursing on L[1:].
Data of unknown depth defeats nested loops
Sketch the pile of nested for loops a list of unknown depth would force, and note the parallel with the nested if/else chains that while loops replaced.