I'm coming from an R background and am confused about the following:
Suppose I had "hello" in list format saved in the variable txt. Thus
txt= ['h','e','l','l','o']
After some testing, the following works:
txt.reverse()
"."join(txt)
and produces as expected olleh.
However, the following does not work:
"".join(txt.reverse())
It gives an error. I'm curious why that is the case as I thought I can "nest" function calls within function calls?
Thank you for helping!
reversedinstead:"".join(reversed(txt)).reversedis in-place. Tryreversed([1,2,3])on the interpreter, it returns alistreverseiteratorobject which means no copies are made. The difference is that.reverse()modifies the original list, whilereversed()iterates over it.reversedreturns an iterator that will return the items in reverse order. It does not modify the original argument, which thereversemethod does.