Idea for improved tuple unpacking

Andy Smith ams70 at cam.ac.uk
Thu Oct 19 23:44:27 EDT 2000


Michael Haggerty <mhagger at alum.mit.edu> writes:

> Hi,
> 
> In my code the following idiom is pretty common:
> 
>     (first,second) = t[:2]
>     rest = t[2:]
> 
> (t is a tuple).  Other scripting languages (Perl, REXX, ...) have a
> way to do this sort of thing in one statement.  Then it occurred to me
> that by analogy with function declaration syntax, it would be cool to
> be able to do
> 
>     (first,second,*rest) = t

While it isn't as short as your suggestion it is possible to do it in
one line with current Python syntax:

  ((first, second), rest) = (t[:2], t[2:])

    Andy
-- 
Andy Smith                                             ams70 at cam.ac.uk
St John's College, Cambridge, CB2 1TP, UK               (07989) 453208




More information about the Python-list mailing list