[Python-Dev] itertools additions: one(), single_valued()

Armin Ronacher armin.ronacher at active-4.com
Mon May 26 21:40:49 CEST 2008


Hi,

Andreas Klöckner <lists <at> informa.tiker.net> writes:

>   list_len = single_valued(len(l) for l in lists)
length, = set(map(len, lists))

>   what_i_am_looking_for = one(item for item in items if predicate(item))
Is that really such a common case?  In Python 2.6 you can use next for the same
thing however without the check if there is just one item:

what_i_am_looking_for = next(filter(predicate, items))

What I would like to see is a batch function defined like that:

def batch(iterable, n):
    return izip(*repeat(iter(iterable), n))

It's a common pattern I use it very often.

Regards,
Armin



More information about the Python-Dev mailing list