Python reverse list

Question:

I’m trying to reverse a string and using the below code but the resultant reverse list value is None.

The code:

str_a = 'This is stirng'
rev_word = str_a.split()
rev_word = rev_word.reverse()
rev_word = ''.join(rev_word)

It returns the TypeError. Why?

Asked By: user1050619

||

Answers:

.reverse() returns None. Therefore you should not be assigning it to a variable.

Use this instead:

stra = 'This is a string'
revword = stra.split()
revword.reverse()
revword=''.join(revword)

I’ve run the code on IDEOne for you so you can see the output. (Also notice that the output is stringaisThis; you may want to use ' '.join(revword), with a space, instead.)

Also note that the method you have provided only reverses the words, not the text. @ron.rothman provided a link that does detail how to reverse a string in its entirety.

Answered By: Cat

This is my personal favorite way to reverse a string:

stra="This is a string"
revword = stra[::-1]

print(revword) #"gnirts a si sihT

or, if you want to reverse the word order:

revword = " ".join(stra.split()[::-1])

print(revword) #"string a is This"

🙂

Answered By: Sean Johnson
>>> s = 'this is a string'
>>> s[::-1]
'gnirts a si siht'
>>> ''.join(reversed(s))
'gnirts a si siht'
Answered By: Andreas Jung

Various reversals on a string:

instring = 'This is a string'
reversedstring = instring[::-1]
print reversedstring        # gnirts a si sihT
wordsreversed = ' '.join(word[::-1] for word in instring.split())
print wordsreversed         # sihT si a gnirts
revwordorder = ' '.join(word for word in instring.split()[::-1])
print revwordorder          # string a is This
revwordandorder = ' '.join(word[::-1] for word in instring.split()[::-1])
print revwordandorder       # gnirts a si sihT
Answered By: Paddy3118

For future reference when an object has a method like [].reverse() it generally performs that action o n the object (ie. the list is sorted and returns nothing, None) in contrast to built in functions like sorted which perform an action on an object and returns a value (ie the sorted list)

Answered By: dm03514

The for loop iterates the string from end (last letter) to start (first letter)

>>> s = 'You can try this too :]'
>>> rev = ''
>>> for i in range(len(s) - 1, -1, -1):
...     rev += s[i]
>>> rev
']: oot siht yrt nac uoY'
Answered By: Aziz Alto

Based on comments and other answers:

str_a = 'this is a string'
rev_word = ' '.join(reversed(str_a.split()))

Method chaining does work in Python after all…

Answered By: user2443147

List reverse can be done using more than one way.
As mentioned in previous answers two are very prominent, one with reverse() function and two with slicing feature. I’m giving some insights on which one we should prefer.
We should always use reverse() function for reversal of a Python list. Two reasons, one in-place reversal and two faster than other.
I’ve some figures to support my answer,

In [15]: len(l)
Out[15]: 1000

In [16]: %timeit -n1 l.reverse()
1 loops, best of 3: 7.87 µs per loop

In [17]: %timeit -n1 l[::-1]
1 loops, best of 3: 10 µs per loop

For 1000 integer list, reverse() function performed better compared to slicing.

Answered By: Aashish P
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.