Re: Is there any nice way to unpack a list of unknown size??

Discussion in 'Python' started by Gary Herron, Sep 14, 2008.

  1. Gary Herron

    Gary Herron Guest

    srinivasan srinivas wrote:
    > I want to do something like below:
    >
    > 1. first, second, third, *rest = foo
    >


    Python 3.0 has exactly this feature. No current Python 2.x version has it.

    Gary Herron

    > 2. for (a,b,c,*rest) in list_of_lists:
    >
    > Please suggest.
    >
    > Thanks,
    > Srini
    >
    >
    > Bring your gang together. Do your thing. Find your favourite Yahoo! group at http://in.promos.yahoo.com/groups/
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Gary Herron, Sep 14, 2008
    #1
    1. Advertising

  2. On Sep 14, 4:08 pm, Gary Herron <> wrote:
    > srinivasan srinivas wrote:
    > > I want to do something like below:

    >
    > > 1. first, second, third, *rest = foo

    >
    > Python 3.0 has exactly this feature.  No current Python 2.x version has it.
    >
    > Gary Herron
    >
    > >  2. for (a,b,c,*rest) in list_of_lists:

    >
    > > Please suggest.

    >
    > > Thanks,
    > > Srini

    >
    > >       Bring your gang together. Do your thing. Find your favourite Yahoo! group athttp://in.promos.yahoo.com/groups/
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list


    In python >= 2.4, you can define a function like this:

    def truncate(iterable, n=1):
    iterator = iter(iterable)
    for i in iterator:
    if n == 0:
    yield iterator
    return
    yield i
    n -= 1

    >>> a, b, c, tail = truncate([1,2,3,4,5,6], 3)
    >>> a

    1
    >>> b

    2
    >>> c

    3
    >>> tail

    <listiterator object at 0x78990>
    >>> list(tail)

    [5, 6]

    --
    Arnaud
     
    Arnaud Delobelle, Sep 14, 2008
    #2
    1. Advertising

  3. Gary Herron

    Tim Chase Guest

    >> I want to do something like below:
    >> 1. first, second, third, *rest = foo

    >
    > Python 3.0 has exactly this feature. No current Python 2.x version has it.


    I asked something similar[1] on c.l.p a while back and Diez
    Roggisch gave this nice workaround/hack[2]

    It's a bit ugly in the implementation (sniffing the stack), but
    elegant in the use, doing exactly what I needed.

    It doesn't accommodate the "rest" portion that you reference, but
    allows for arbitrary unpacking into a fixed-length tuple.

    -tkc


    [1]
    http://mail.python.org/pipermail/python-list/2006-May/381386.html

    [2]
    http://mail.python.org/pipermail/python-list/2006-May/381399.html
     
    Tim Chase, Sep 14, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DeadInPlastic
    Replies:
    1
    Views:
    817
    Karl Heinz Buchegger
    Jun 26, 2003
  2. srinivasan srinivas
    Replies:
    0
    Views:
    263
    srinivasan srinivas
    Sep 14, 2008
  3. Chris Bardon

    Is there a nice way to dynamically call a web service?

    Chris Bardon, Apr 29, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    155
    Andrew Hopper
    Apr 29, 2004
  4. Kenneth McDonald

    Any nice way to get a temporary directory?

    Kenneth McDonald, Oct 23, 2008, in forum: Ruby
    Replies:
    7
    Views:
    185
    Nobuyoshi Nakada
    Oct 24, 2008
  5. Replies:
    5
    Views:
    360
Loading...

Share This Page