chop() and empty() functions

Jeremy L. Moles jeremy at
Fri May 26 18:54:20 CEST 2006

I've been using the following lambda/function for a number of months now
(I got the idea from someone in #python, though I don't remember who):

def chop(s, n):
        """Chops a sequence, s, into n smaller tuples."""

        return zip(*[iter(s)] * n)


chop = lambda s, n: zip(*[iter(s)] * n)

I was wondering if something like this already exists in Python proper
somewhere, perhaps in an optimized way? (I checked itertools, but I
didn't see anything right away).

Furthermore, what do people think about the idea of adding a truly
empty, no-op global lambda somewhere in Python? I use them a lot
(usually defining a: empty = lambda *a, **k: None somewhere at the
topmost module space), but if enough people did too, it might be worth
adding an empty() builtin to much later versions of Python.

Anyways, if these are stupid ideas I'm not surprised. Just some things
I've been thinking about and figured I'd toss out there on my lunch
break. :)

More information about the Python-list mailing list