Ask Your Question
1

How reverse a list with Sage?

asked 2013-08-01 22:42:57 -0600

petropolis gravatar image

See on http://www.sagemath.org/doc/thematic_...

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.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-08-01 23:23:55 -0600

tmonteil gravatar image

updated 2013-08-01 23:37:55 -0600

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 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]
edit flag offensive delete link more

Comments

Thanks! Somehow I am used to assume that L.reverse() returns the reversed L. Am I the only one who finds this behaviour more natural?

petropolis gravatar imagepetropolis ( 2013-08-02 00:31:14 -0600 )edit

I 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.

tmonteil gravatar imagetmonteil ( 2013-08-02 01:30:42 -0600 )edit
2

answered 2013-08-02 00:14:34 -0600

To 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.

edit flag offensive delete link more

Comments

1

Thanks Samuel. And to complement it, is seems that `reversed(L)` is faster than `L[::-1]` which is faster than `list(reversed(L))`.

tmonteil gravatar imagetmonteil ( 2013-08-02 01:32:07 -0600 )edit

"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.

petropolis gravatar imagepetropolis ( 2013-08-02 22:47:04 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-08-01 22:42:57 -0600

Seen: 2,166 times

Last updated: Aug 02 '13