I'm applying the reverse
method to a list and it returns None
.
Ex.
teste = ['audi', 'subaru', 'fiat', 'ford']
print(teste.reverse())
None
I'm applying the reverse
method to a list and it returns None
.
Ex.
teste = ['audi', 'subaru', 'fiat', 'ford']
print(teste.reverse())
None
Whenever you are in doubt, consult the documentation!
Consider the documentation for reverse
:
list.reverse ()
Reverse the elements of the list in place.
What translated means: Reverses the elements directly in the list. But it does not say it returns something, so it does not return anything. The documentation is always clear on the returns indicating which is always where there is one. See the% of 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 uses the list after it has been inverted. If you only need the inverted list for an operation, and it is not permanent, you can do it with slicing and negative :
>>> teste = ['audi', 'subaru', 'fiat', 'ford']
>>> print(teste[::-1])
['ford', 'fiat', 'subaru', 'audi']
Now I'm reminded that this is slightly different from what you have in the question because in my example the list was not inverted, I just used the list inversion in print
, but the original list stays the same. In many scenarios this serves the purpose, but in others it may not.
The list.reverse
method, as well as list.sort
, in-place , that is, they change the original list instead of returning an altered copy.
If you do not want to change the original list you can use reversed()
functions and sorted()
that have the same function but return an iterator
, without changing the original list.
You can use the iterator if you do not need to read the values more than once, but if you need to, you can convert the iterator to a list
using teste = list(iterador)
. It may be less performative depending on the size of the rolled-in list, but for small lists it does not make much of an 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']
How it works:
teste = ['audi', 'subaru', 'fiat', 'ford']
teste.reverse()
print(teste)
See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .
The reverse()
function does not return a new list set to what it wants, it manipulates the current list and returns nothing even, the operation is performed directly on the object in question. Then you have the object printed and everything is okay.
If you really want to get something back for direct use you should use the sorted()
function. It is useful if you only need it once, or if you need it not to touch the original. If you use this function it may be useful to record to a variable for later use. It will occupy extra memory. The same can be said of reversed()
which is more complicated to use, I would not even go this way.