By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten)

P: n/a
ge******* wrote:
window / cons / fencepost / slice functions: +1

(with a flag to say if you want to truncate or pad incomplete tuples
at end of input sequence.

Probably more recipes in there, (and not CPAN-ish yet) but multiple
submissions bespeak a certain need, i think.

Yes, also worth noting is the thread:

which concludes with:

from itertools import islice, chain, repeat

def partition(iterable, part_len):
itr = iter(iterable)
while 1:
item = tuple(islice(itr, part_len))
if len(item) < part_len:
raise StopIteration
yield item

def padded_partition(iterable, part_len, pad_val=None):
padding = repeat(pad_val, part_len-1)
itr = chain(iter(iterable), padding)
return partition(itr, part_len)

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.