I'm assuming the rule is "print every item in list l
or nested lists within l
on its own line, unless a nested contains no sub-lists, in which case, print all its items on one line with spaces between."
If that's right, then this code should work:
l = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']
def print_list(lst):
if not any(isinstance(x, list) for x in lst):
# print list of strings on one line
print(*lst)
return
# contains sublists; print or process items one by one
for x in lst:
if isinstance(x, list):
print_list(x) # process sub-list
else:
print(x)
print_list(l)