[Tutor] rotating a list

Gregor Lingl glingl at aon.at
Sun Sep 21 16:58:12 EDT 2003


Charlie Clark schrieb:

> ... *grin*
>
>As usual Danny's solution is elegant an conclusive but his catch-all for 
>empty lists is maybe a bit too much. I actually found all of the solutions 
>too mathematic and not expressive enuff so came up with the following:
>  
>
>>>>l = [1, 2, 3]
>>>>l.append(l.pop(0)) 
>>>>        
>>>>
>
>This will give IndexError for empty lists but with a helpful error message 
>telling you the list is empty. 
>
Hi, Charlie,
you can easily amend this (in Dannies sense) by writing:

if l: l.append(l.pop())

>You can also rotate at different positions 
>using different values of pop()
>
hmm..., how do you mean should this be done?

> and you're using real list methods so it's 
>nice OO, I think.
>  
>
O.k. And, moreover it's MUCH faster for long list, already
nearly twice as fast for lists with 100 elements.

Thanks for this idea,
Gregor

>Charlie
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>





More information about the Tutor mailing list