Best idiom to unpack a variable-size sequence

ram rickmorrison at gmail.com
Tue Dec 18 12:49:35 EST 2007


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



More information about the Python-list mailing list