Best idiom to unpack a variable-size sequence

Discussion in 'Python' started by ram, Dec 18, 2007.

  1. ram

    ram Guest

    Here's a little issue I run into more than I like: I often need to
    unpack a sequence that may be too short or too long into a fixed-size
    set of items:

    a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)

    What I usually do is something like this:

    a, b, c = (list(seq) + [None, None, None])[:3]

    but that just feels rather ugly to me -- is there a good Pythonic
    idiom for this?

    Thx,
    Rick
    ram, Dec 18, 2007
    #1
    1. Advertising

  2. On Dec 18, 12:49 pm, ram <> wrote:
    > Here's a little issue I run into more than I like: I often need to
    > unpack a sequence that may be too short or too long into a fixed-size
    > set of items:
    >
    > a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)
    >
    > What I usually do is something like this:
    >
    > a, b, c = (list(seq) + [None, None, None])[:3]
    >
    > but that just feels rather ugly to me -- is there a good Pythonic
    > idiom for this?


    In terms of brevity I don't think so; however it can be done more
    efficient and general (e.g. for infinite series) using itertools:

    from itertools import islice, chain, repeat

    def unpack(iterable, n, default=None):
    return islice(chain(iterable,repeat(default)), n)

    a, b, c = unpack(seq, 3)

    George
    George Sakkis, Dec 18, 2007
    #2
    1. Advertising

  3. ram

    Duncan Booth Guest

    ram <> wrote:

    > Here's a little issue I run into more than I like: I often need to
    > unpack a sequence that may be too short or too long into a fixed-size
    > set of items:
    >
    > a, b, c = seq # when seq = (1, 2, 3, 4, ...) or seq = (1, 2)
    >
    > What I usually do is something like this:
    >
    > a, b, c = (list(seq) + [None, None, None])[:3]
    >
    > but that just feels rather ugly to me -- is there a good Pythonic
    > idiom for this?


    Pythonic might be to be explicit: i.e. know in advance how long the
    sequence actually is.

    One drawback I see with your code is that it doesn't give you any way to
    specify different defaults for the values. So here's an alternative to
    consider: try passing your sequence to a function. This lets you specify
    appropriate defaults, and it reads quite cleanly. Of course it also forces
    you to extract the code using those variables out into a separate function,
    but that may not be a bad thing.

    >>> def process(a=None, b=None, c=None):

    print a, b, c


    >>> seq = iter('abcd')
    >>> process(*itertools.islice(seq,0,3))

    a b c
    >>> seq = iter('ab')
    >>> process(*itertools.islice(seq,0,3))

    a b None
    >>>
    Duncan Booth, Dec 18, 2007
    #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. ahsan Imam
    Replies:
    4
    Views:
    5,719
    Josiah Carlson
    Feb 7, 2004
  2. Tung Wai Yip

    unpack tuple of wrong size

    Tung Wai Yip, Apr 6, 2004, in forum: Python
    Replies:
    2
    Views:
    529
    Duncan Booth
    Apr 7, 2004
  3. stef mientki
    Replies:
    13
    Views:
    634
    stef mientki
    Oct 20, 2007
  4. srinivasan srinivas
    Replies:
    0
    Views:
    247
    srinivasan srinivas
    Sep 14, 2008
  5. Gary Herron
    Replies:
    2
    Views:
    312
    Tim Chase
    Sep 14, 2008
Loading...

Share This Page