Python Reverse List

I am trying to change the line and use the code below, but the resulting value of the reverse list 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 a TypeError . Why?

+43
python
Sep 09 '12 at 2:40
source share
8 answers

.reverse() returns None . Therefore, you should not assign it to a variable.

Use this instead:

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

I ran the code in IDEOne for you so you can see the result. (Also note that the output is stringaisThis , you can use ' '.join(revword) instead of a space.)

Also note that the method you provided cancels words , not text . @ ron.rothman provided a link that details how to reverse a string.

+50
09 Sep
source share
โ€” -

This is my personal favorite way to change the line:

 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" 

:)

+114
Sep 09 '12 at 2:52
source share

Different line spreads:

 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 
+7
Sep 09 '12 at 5:10
source share

In the future, when an object has a method such as [].reverse() , it usually performs this action on the object (that is, the list is sorted and returns nothing, None), unlike built-in functions such as sorted , which execute action on the object and return a value (i.e. a sorted list)

+6
09 Sep '12 at 5:20
source share
 >>> s = 'this is a string' >>> s[::-1] 'gnirts a si siht' >>> ''.join(reversed(s)) 'gnirts a si siht' 
+3
Sep 09 '12 at 5:01
source share

The for loop iterates over the line from the end (last letter) to the beginning (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' 
0
May 08 '14 at 7:38
source share

Based on comments and other answers:

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

The method chain really works in Python after all ...

0
Aug 01 '14 at 15:42
source share

The list can be reversed in several ways.
As mentioned in previous answers, two are very outstanding, one with a reverse() function and two with a slice function. I talk about which one we would prefer. We should always use the reverse() function to access a Python list. Two reasons: one U-turn in place and two faster than the others. I have numbers 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 whole lists, the reverse() function performs better than slicing.

0
Dec 29 '15 at 9:44
source share



All Articles