Method Reverse returns None

Asked

Viewed 691 times

2

I’m applying the method reverse in a list and the same returns None.

Ex.

teste = ['audi', 'subaru', 'fiat', 'ford']
print(teste.reverse())

None

5 answers

6

Whenever in doubt consult the documentation!

Note the documentation for the reverse:

list.()

Reverse the Elements of the list in place.

Translated means: it inverts the elements directly in the list. But it does not say that it returns anything, so it returns nothing. The documentation is always clear in the returns indicating which is where there is one. See the count for example:

list.Count(x)

Return the number of times x appears in the list.

In the code that has the most natural is to do as @Maniero showed in what it does reverse and then use the list after it’s been reversed. If you only need the inverted list for an operation, which is not permanent, you can do so with slicing and step negative:

>>> teste = ['audi', 'subaru', 'fiat', 'ford']
>>> print(teste[::-1])
['ford', 'fiat', 'subaru', 'audi']

Now I remind you that this is slightly different from the question because in my example the list was not reversed, I only used the inversion of the list in print, but the original list remains the same. In many scenarios this serves the goal, but in others it may not serve.

5

The method list.reverse, as well as the list.sort, work in-place, i.e., they change the original list instead of returning an altered copy.

If you do not want to change the original list you can use the functions reversed() and sorted() that have the same function but return a iterator, without changing the original list.

You can use the iterator if you don’t need to read the values more than once, but if you need it you can convert the iterator to one list using teste = list(iterador). It may be less performative depending on the size of the reverse list, but for small lists it has little impact.

teste = ['audi', 'subaru', 'fiat', 'ford']

rev_iterator = reversed(teste) # Iterator que itera a lista de trás pra frente
rev = list(rev_iterator)       # consumindo o iterator e atribuindo a uma lista

print(rev_iterator)  # <list_reverseiterator object at 0x7fdd5beea6a0>
print(rev)           # ['ford', 'fiat', 'subaru', 'audi']

Repl.it code working

5

That’s how it works:

teste = ['audi', 'subaru', 'fiat', 'ford']
teste.reverse()
print(teste)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The function reverse() does not return a new list adjusted to what it wants, it handles the current list and returns nothing at all, the operation is performed directly on the object in question. Then you have print the object and leave everything ok.

If you really want to return something for direct use you should use the function sorted(). It’s useful if you only need it once, or if you need it to leave the original alone. Using this function can be useful to save to a variable for later use. It will take up extra memory. The same can be said for reversed() which is more complicated to use, I wouldn’t even go this way.

1

My friend, your question is old, but as many people still have doubt, I will answer with two different methods for python 3:

teste = ['audi', 'subaru', 'fiat', 'ford']

print(sorted(teste, reverse=True))

or

print(lista.sort(reverse=True)

remembering that Sort saves the modified list and Sorted does not, ok?

knoppix@Microknoppix:~$ python3 $a
['subaru', 'ford', 'fiat', 'audi']

0

That’s how it works, old boy:

def lista_reversa(x):
        x.reverse()
        return x

lista = ["a", "b", "c"]
lista = lista_reversa(lista)
print(lista) # ['c', 'b', 'a']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.