Why doesn't list.reverse return a list?
list.reverse
is an inplace operation, so it will change the list and return None
. You should be using reversed
function, like this
"".join(reversed(rst))
I would personally recommend using slicing notation like this
rst[::-1]
For example,
rst = "cabbage"
print "".join(reversed(rst)) # egabbac
print rst[::-1] # egabbac
Have you tried the following?
"".join(s for s in reversed(st))
reversed
returns a reverse iterator. Documentation is here
It fails because lst.reverse()
reverses a list in place and returns None
(and you cannot iterate over None
). What you are looking for is (for example) reversed(lst)
which creates a new list out of lst
which is reversed.
Note that if you want to reverse a string then you can do that directly (without lists):
>>> st = "This is Ok"
>>> st[::-1]
"kO si sihT"