[Python-ideas] [Python-Dev] Proposal: new list function: pack

paul bedaride paul.bedaride at gmail.com
Fri Mar 20 15:42:51 CET 2009

Yes it's true you can do easily the pack part with zip(*[iter(l)]*size)
you can do the slicing with zip(*[l[i:len(l)-(slice-1-i)] for i in
And you could also do the twice but you get something more complicated.
It's also true that with izip you could get iterator.
I use this pack function a lot of times in my code, and it's more readable,
than the zip version. After, the thing it's to know if people really
use this kind
 of function on list of it's just me (that's it's totally possible).

paul bedaride

On Fri, Mar 20, 2009 at 3:07 PM, Isaac Morland <ijmorlan at uwaterloo.ca> wrote:
> On Fri, 20 Mar 2009, paul bedaride wrote:
>> I propose a new function for list for pack values of a list and
>> sliding over them:
>> then we can do things like this:
>> for i, j, k in pack(range(10), 3, partialend=False):
>>   print i, j, k
>> I propose this because i need a lot of times pack and slide function
>> over list and this one
>> combine the two in a generator way.
> See the Python documentation for zip():
> http://docs.python.org/library/functions.html#zip
> And this article in which somebody independently rediscovers the idea:
> http://drj11.wordpress.com/2009/01/28/my-python-dream-about-groups/
> Summary: except for the "partialend" parameter, this can already be done in
> a single line.  It is not for me to say whether this nevertheless would be
> useful as a library routine (if only perhaps to make it easy to specify
> "partialend" explicitly).
> It seems to me that sometimes one would want izip instead of zip.  And I
> think you could get the effect of partialend=True in 2.6 by using
> izip_longest (except with an iterator result rather than a list).
>> def pack(l, size=2, slide=2, partialend=True):
>>   lenght = len(l)
>>   for p in range(0,lenght-size,slide):
>>       def packet():
>>           for i in range(size):
>>               yield l[p+i]
>>       yield packet()
>>   p = p + slide
>>   if partialend or lenght-p == size:
>>       def packet():
>>           for i in range(lenght-p):
>>               yield l[p+i]
>>       yield packet()
> Isaac Morland                   CSCF Web Guru
> DC 2554C, x36650                WWW Software Specialist

More information about the Python-ideas mailing list