I'm trying to find a way to flatten my list so it outputs:
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
So far my recursive function returns a new nested list of values with matched keys in a dictionary (using an old parameter list and a dictionary) is as follows:
l = ['won', 'too', 'three', 'fore', 'five', 'six', 'seven', 'ate', 'nein']
d = dict(won='one', too='two', fore='four', ate='eight', nein='nine')
def subst(l,d):
if len(l) == 1:
if l[0] in d:
return d[l[0]]
else:
return l[0]
else:
return [d[l[0]] if l[0] in d else l[0]] + [subst(l[1:],d)]
So far I've been getting:
['one', ['two', ['three', ['four', ['five', ['six', ['seven', ['eight', 'nine']]]]]]]]
Is there any way I can flatten the list while keeping the recursive integrity of the function?