One of the questions I had on an exam was to write a python program only using recursion (no loops were allowed). The goal was to convert a list which contained both integers and lists (which also only contained integers) and make a single list only containing these integers.
Everything works fine in the code below until it encounters a list: after this it just stops. The fix has to be fairly simple but I just can't find it.
a = [1,5,2,[3,4],6]
def list_in_list(l, i = 0):
if i >= len(l) - 1:
return [l[i]] if type(l[i]) == int else list_in_list(l[i], i=0)
elif type(l[i]) == list:
return list_in_list(l[i],i=0)
return [l[i]] + list_in_list(l, i+1)
print(list_in_list(a))