# Revision history [back]

The method .reverse() reverses the list L on place (i mean it modifies the list L), it does not return a sorted copy of L (it returns nothing, hence your behaviour), but the list L itself is modified as you can check:

sage: L = [4,2,5,1,3] sage: L.reverse() sage: L [3, 1, 5, 2, 4]

The method .reverse() reverses the list L on place (i mean it modifies the list L), it does not return a sorted copy of L (it returns nothing, hence your behaviour), but the list L itself is modified as you can check:

sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4] 4]


Now if you want to get a reversed copy of L without modifying L, you can use the reversed() function.

sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>


As you can see, it returns an iterator, not a list, it means that you can still play with it:

sage: for i in L2:
....:     print i
....:
3
1
5
2
4


If you want to get a reversed list of L without modifying L you can transform the reversed iterator into a list:

sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]


The method .reverse() reverses the list L on place (i mean it modifies the list L), it does not return a sorted copy of L (it returns nothing, hence your behaviour), but the list L itself is modified as you can check:

sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4]


Now if you want to get a reversed copy of L without modifying L, you can use the reversed() function.function:

sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>


As you can see, it returns an iterator, not a list, it means that you can still play with it:

sage: for i in L2:
....:     print i
....:
3
1
5
2
4


If you want to get a reversed list of L without modifying L you can transform the reversed iterator into a list:

sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]


The method .reverse() reverses the list L on place (i mean it (it modifies the list L), as stated in the tutorial), it does not return a sorted reversed copy of L (it returns nothing, hence your behaviour), but the list L itself is modified as you can check:

sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4]


Now if you want to get a reversed copy of L without modifying L, you can use the reversed() function:

sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>


As you can see, it returns an iterator, not a list, it means that you can still play with it:

sage: for i in L2:
....:     print i
....:
3
1
5
2
4


If you want to get a reversed list of L without modifying L you can transform the reversed iterator into a list:

sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]


The method .reverse() reverses the list L on place (it modifies the list L as stated in the tutorial), it does not return a reversed copy of L (it returns nothing, hence your behaviour), but the list L itself is modified as you can check:

sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4]


Now if you want to get a reversed copy of L without modifying L, you can use the reversed() function:

sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>


As you can see, it returns an iterator, not a list, it means that you can still play with it:it as if it was a list, but only once (the elements of L2 are thrown once used):

sage: for i in L2:
....:     print i
....:
3
1
5
2
4
sage: for i in L2:
....:     print i
....:
<nothing printed>


If you want to get a reversed list of L without modifying L you can transform the reversed iterator into a list:

sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]