ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 03 Aug 2013 05:47:04 +0200How reverse a list with Sage?https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/See on http://www.sagemath.org/doc/thematic_tutorials/tutorial-programming-python.html
From the section "Modifying lists: reverse, sort, ...":
L = [4,2,5,1,3]
L.reverse()
sage: [3, 1, 5, 2, 4]
However I see -- nothing.
print L.reverse()
gives 'None'. I am using Sage 5.8.Fri, 02 Aug 2013 05:42:57 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/Answer by slelievre for <p>See on <a href="http://www.sagemath.org/doc/thematic_tutorials/tutorial-programming-python.html">http://www.sagemath.org/doc/thematic_...</a></p>
<p>From the section "Modifying lists: reverse, sort, ...":</p>
<p>L = [4,2,5,1,3]</p>
<p>L.reverse()</p>
<p>sage: [3, 1, 5, 2, 4]</p>
<p>However I see -- nothing.</p>
<p>print L.reverse()</p>
<p>gives 'None'. I am using Sage 5.8.</p>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?answer=15302#post-id-15302To complement Thierry's answer, here is another way to get a reversed list:
sage: L = [4,2,5,1,3]
sage: L[::-1]
[3, 1, 5, 2, 4]
This produces a new list. The original list is unchanged.
Fri, 02 Aug 2013 07:14:34 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?answer=15302#post-id-15302Comment by tmonteil for <p>To complement Thierry's answer, here is another way to get a reversed list:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L[::-1]
[3, 1, 5, 2, 4]
</code></pre>
<p>This produces a new list. The original list is unchanged.</p>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17197#post-id-17197Thanks Samuel. And to complement it, is seems that `reversed(L)` is faster than `L[::-1]` which is faster than `list(reversed(L))`.Fri, 02 Aug 2013 08:32:07 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17197#post-id-17197Comment by petropolis for <p>To complement Thierry's answer, here is another way to get a reversed list:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L[::-1]
[3, 1, 5, 2, 4]
</code></pre>
<p>This produces a new list. The original list is unchanged.</p>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17195#post-id-17195"it seems that reversed(L) is faster.." Of course, because it just returns a pointer to a function but does reverse anything. I am still very unhappy with this counterintuitive parlance which is also not in accordance with English language.Sat, 03 Aug 2013 05:47:04 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17195#post-id-17195Answer by tmonteil for <p>See on <a href="http://www.sagemath.org/doc/thematic_tutorials/tutorial-programming-python.html">http://www.sagemath.org/doc/thematic_...</a></p>
<p>From the section "Modifying lists: reverse, sort, ...":</p>
<p>L = [4,2,5,1,3]</p>
<p>L.reverse()</p>
<p>sage: [3, 1, 5, 2, 4]</p>
<p>However I see -- nothing.</p>
<p>print L.reverse()</p>
<p>gives 'None'. I am using Sage 5.8.</p>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?answer=15301#post-id-15301The 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 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]
Fri, 02 Aug 2013 06:23:55 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?answer=15301#post-id-15301Comment by tmonteil for <p>The method <code>.reverse()</code> reverses the list <code>L</code> on place (it modifies the list <code>L</code> as stated in the tutorial), it does not return a reversed copy of <code>L</code> (it returns nothing, hence your behaviour), but the list <code>L</code> itself is modified as you can check:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4]
</code></pre>
<p>Now if you want to get a reversed copy of <code>L</code> without modifying <code>L</code>, you can use the <code>reversed()</code> function:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>
</code></pre>
<p>As you can see, it returns an iterator, not a list, it means that you can still play with it as if it was a list, but only once (the elements of <code>L2</code> are thrown once used):</p>
<pre><code>sage: for i in L2:
....: print i
....:
3
1
5
2
4
sage: for i in L2:
....: print i
....:
<nothing printed>
</code></pre>
<p>If you want to get a reversed list of <code>L</code> without modifying <code>L</code> you can transform the reversed iterator into a list:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]
</code></pre>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17198#post-id-17198I think it is about english language: `reverse` reverses the list `reversED` returns the reversed list. As for me, the main "issue" is that `reversed()` is a function, not a method, which you can not guess by tab completion. By the way, both features are Python (not Sage) builtins.Fri, 02 Aug 2013 08:30:42 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17198#post-id-17198Comment by petropolis for <p>The method <code>.reverse()</code> reverses the list <code>L</code> on place (it modifies the list <code>L</code> as stated in the tutorial), it does not return a reversed copy of <code>L</code> (it returns nothing, hence your behaviour), but the list <code>L</code> itself is modified as you can check:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L.reverse()
sage: L
[3, 1, 5, 2, 4]
</code></pre>
<p>Now if you want to get a reversed copy of <code>L</code> without modifying <code>L</code>, you can use the <code>reversed()</code> function:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L2 = reversed(L)
sage: L2
<listreverseiterator at 0x65a5d50>
</code></pre>
<p>As you can see, it returns an iterator, not a list, it means that you can still play with it as if it was a list, but only once (the elements of <code>L2</code> are thrown once used):</p>
<pre><code>sage: for i in L2:
....: print i
....:
3
1
5
2
4
sage: for i in L2:
....: print i
....:
<nothing printed>
</code></pre>
<p>If you want to get a reversed list of <code>L</code> without modifying <code>L</code> you can transform the reversed iterator into a list:</p>
<pre><code>sage: L = [4,2,5,1,3]
sage: L2 = list(reversed(L))
sage: L2
[3, 1, 5, 2, 4]
</code></pre>
https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17201#post-id-17201Thanks! Somehow I am used to assume that L.reverse() returns the reversed L. Am I the only one who finds this behaviour more natural? Fri, 02 Aug 2013 07:31:14 +0200https://ask.sagemath.org/question/10402/how-reverse-a-list-with-sage/?comment=17201#post-id-17201