Python reverse list

Python

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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"

:)

Solution 2 - Python

.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.

Solution 3 - Python

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

Solution 4 - Python

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)

Solution 5 - Python

>>> s = 'this is a string'
>>> s[::-1]
'gnirts a si siht'
>>> ''.join(reversed(s))
'gnirts a si siht'

Solution 6 - Python

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'

Solution 7 - Python

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...

Solution 8 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1050619View Question on Stackoverflow
Solution 1 - PythonSean JohnsonView Answer on Stackoverflow
Solution 2 - PythonCatView Answer on Stackoverflow
Solution 3 - PythonPaddy3118View Answer on Stackoverflow
Solution 4 - Pythondm03514View Answer on Stackoverflow
Solution 5 - PythonAndreas JungView Answer on Stackoverflow
Solution 6 - PythonAziz AltoView Answer on Stackoverflow
Solution 7 - Pythonuser2443147View Answer on Stackoverflow
Solution 8 - PythonAashish PView Answer on Stackoverflow