Lambda question

jyoung79 at kc.rr.com jyoung79 at kc.rr.com
Sat Jun 4 13:46:23 EDT 2011


I was surfing around looking for a way to split a list into equal sections.  I 
came upon this algorithm: 
 
>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc 
>>> f("Hallo Welt", 3) 
['Hal', 'lo ', 'Wel', 't'] 
 
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-s
ized-chunks-in-python/312644
 
It doesn't work with a huge list, but looks like it could be handy in certain 
circumstances.  I'm trying to understand this code, but am totally lost.  I 
know a little bit about lambda, as well as the ternary operator, but how 
does this part work: 
 
>>> f('dude'[3:], 3, []+[('dude'[:3])]) 
['dud', 'e'] 
 
Is that some sort of function call, or something else?  I'm guessing it works 
recursively? 
 
Just curious if anyone could explain how this works or maybe share a link 
to a website that might explain this? 
 
Thanks. 
 
Jay 



More information about the Python-list mailing list