Here is the code I'm currently working with.
Input = ['cat','tab','mad dog']
output = [x for x in Input] + [' '.join(x.split(' ')[::-1]) for x in Input if len(x.split())>1]
It works fine and is pretty fast but I don't think it reads particularly well.
Is there anything better that wouldn't sacrifice speed, as that is the most important thing for its use?
Why? its actually for searching directories where the name (one or two words) may have been typed in reverse.
output= ['cat', 'tab', 'mad dog', 'dog mad']
This is the correct result but I'm pretty sure there is a better way of getting there.
Thank you.
as #slothrop commented
Input = ['cat','tab','mad dog']
output = Input + [' '.join(x.split()[::-1]) for x in Input if ' ' in x]
or
Input.append(*[' '.join(x.split()[::-1]) for x in Input if ' ' in x])
or
output = Input + [' '.join(reversed(x.split())) for x in Input if ' ' in x]
#['cat', 'tab', 'mad dog', 'dog mad']