In general, when you have to deal with arbitrary levels of nesting, a recursive solution is a good fit. Lists within lists, parsing HTML (tags within tags), working with filesystems (directories within directories), etc.
I haven't tested this code extensively, but I believe it should do what you want:
ll = [ 1, 2, 3, [4, 5, [6, 7, 8]]]
def flatten(input_list):
output_list = []
for element in input_list:
if type(element) == list:
output_list.extend(flatten(element))
else:
output_list.append(element)
return output_list
print (flatten(ll)) #prints [1, 2, 3, 4, 5, 6, 7, 8]
In general recursion is very easy to think about and the solutions tend to be very elegant (like above) but for really, really deeply nested things - think thousands of levels deep - you can run into problems like stack overflow.
Generally this isn't a problem, but I believe a recursive function can always* be converted to a loop (it just doesn't look as nice.)
- Note: I am not crash-hot on my compsci theory here. Someone can add details or correct me if I'm wrong.