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

paul bedaride paul.bedaride at gmail.com
Fri Mar 20 14:35:10 CET 2009

```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.

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()

a = range(10)
print a
print 'pack(a, 2, 2,  True):',  [list(p) for p in pack(a, 2, 2, True)]
print 'pack(a, 2, 2, False):',  [list(p) for p in pack(a, 2, 2, False)]
print 'pack(a, 2, 3,  True):', [list(p) for p in pack(a, 2, 3, True)]
print 'pack(a, 2, 3, False):', [list(p) for p in pack(a, 2, 3, False)]
print 'pack(a, 3, 2,  True):', [list(p) for p in pack(a, 3, 2, True)]
print 'pack(a, 3, 2, False):', [list(p) for p in pack(a, 3, 2, False)]
print 'pack(a, 3, 3,  True):', [list(p) for p in pack(a, 3, 3, True)]
print 'pack(a, 3, 3, False):', [list(p) for p in pack(a, 3, 3, False)]

paul bedaride
```