unary star

sismex01 at hebmex.com sismex01 at hebmex.com
Tue May 6 10:26:24 EDT 2003


> From: VanL [mailto:vlindberg at verio.net]
> Sent: Tuesday, May 06, 2003 9:20 AM
> 
> I like this idea -- I frequently find myself in situations 
> where I want to take only the first item from a list. 
> (I usually reverse the list, and then pop() it.)
> 
> I would vastly prefer a
> 
> first, *rest = some_list
> 
> type syntax.
>

Why contaminate with cruft a simple yet flexible language?
This function does exactly what you want:

def Peel(items):
    """
    Peel(list) -> item, rest

    Returns a tuple consisting of the first item
    on the list, and a list with the rest of the
    items.
    """
    return items[0], items[1:]

and you don't need to reverse(), pop(), reverse().
You do know that you can use pop(x), with x=0, to
effectively pop the first item on a list?

-gustavo





More information about the Python-list mailing list