In python, I have this list containing
['HELLO', 'WORLD']
how do I turn that list into
['OLLEH', 'DLROW']
>>> words = ['HELLO', 'WORLD']
>>> [word[::-1] for word in words]
['OLLEH', 'DLROW']
Arguably using the builtin reversed is more clear than slice notation x[::-1].
[reversed(word) for word in words]
or
map(reversed, words)
Map is fast without the lambda.
I just wish it was easier to get the string out of the resulting iterator. Is there anything better than ''.join() to put together the string from the iterator?
''.join to put it together[''.join(reversed(word)) for word in words] is the full version of your first solution, which is valid and some argue it is more readable than [::-1], but I am of the opinion that the reverse slice is much betterUsing map and lambda(lambdas are slow):
>>> lis=['HELLO', 'WORLD']
>>> map(lambda x:x[::-1],lis)
['OLLEH', 'DLROW']