Re: unpacking first few items of iterable

Discussion in 'Python' started by Mitya Sirenef, Dec 13, 2012.

  1. On 12/13/2012 03:09 PM, Daniel Fetchinson wrote:
    >>> Hi folks, I swear I used to know this but can't find it anywhere:
    >>>
    >>> What's the standard idiom for unpacking the first few items of an
    >>> iterable whose total length is unknown?
    >>>
    >>> Something like
    >>>
    >>> a, b, c, _ = myiterable
    >>>
    >>> where _ could eat up a variable number of items, in case I'm only
    >>> interested in the first 3 items?

    >> If you're using python3, you can simply do:
    >>
    >> a, b, c, *rest = myiterable

    > Thanks, sounds great, how about python2?
    >


    If you know the sequence has at least n items, you
    can do a, b, c = seq[:3]

    -m

    --
    Lark's Tongue Guide to Python: http://lightbird.net/larks/
     
    Mitya Sirenef, Dec 13, 2012
    #1
    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. Daniel Fetchinson

    unpacking first few items of iterable

    Daniel Fetchinson, Dec 13, 2012, in forum: Python
    Replies:
    0
    Views:
    124
    Daniel Fetchinson
    Dec 13, 2012
  2. Demian Brecht

    Re: unpacking first few items of iterable

    Demian Brecht, Dec 13, 2012, in forum: Python
    Replies:
    0
    Views:
    127
    Demian Brecht
    Dec 13, 2012
  3. MRAB
    Replies:
    0
    Views:
    135
  4. Daniel Fetchinson

    Re: unpacking first few items of iterable

    Daniel Fetchinson, Dec 13, 2012, in forum: Python
    Replies:
    0
    Views:
    138
    Daniel Fetchinson
    Dec 13, 2012
  5. Daniel Fetchinson

    Re: unpacking first few items of iterable

    Daniel Fetchinson, Dec 13, 2012, in forum: Python
    Replies:
    0
    Views:
    126
    Daniel Fetchinson
    Dec 13, 2012
Loading...

Share This Page