But sometimes I have to wonder... For example, here is a question from yesterday about how to group elements from a sequence (and since it's from yesterday, it is naturally a duplicate). The answer that people like has me scratching my head. Here is how I do it:
I like this because: (a) it works, and (b) it's simple enough to understand at a glance. For very long sequences you might want to use xrange within the list comprehension. Here is the popular answer:
This is just crazy. It is not at all clear how this works or what it does. It would have to be commented in code. And, how does it work? The docs are here. We first make a list of
iter(L)
objects:But when I try to unpack it with *, I get:
So the bottom line is that I don't really know how it works because I can't take it apart.
What
izip_longest
does is take a list of lists and pop items off each one in turn to put into the groups. From the docs:izip_longest('ABCD', 'xy', fillvalue='-')
--> Ax By C- D-
It seems like a lot of extra work is being done here, to go along with the obfuscation. These guys should go back to Perl!