I'm new to python so please excuse the layman description.
Question : Given a sentence, return a sentence with the words reversed
My Solution:
def sentence_reverse(text):
a = text.split()
a.reverse()
return ' '.join(a)
Output:
sentence_reverse('I am home') --> 'home am I'
sentence_reverse('We are ready') --> 'ready are We'
the code works the way it is supposed to but my question is, why can't i make a function call directly from the returned result of the previous function. i,e
why can i not do something like
return ' '.join(text.split().reverse())
this would work just fine in java since i get List object from text.split() to which, i would operate on with the reverse() function which would return me the inverted list which would then be used by the join() to make this list into a string.
This, is also something that i have tried:
text.split() returns a List, and applying .reverse() function on it SHOULD return the elements of the same list in reverse order, but when i execute this line,print(text.split().reverse())
, i get None