[Tutor] split a tuple

János Juhász janos.juhasz at VELUX.com
Thu Nov 17 08:27:32 CET 2005


Hi,

I couldn't get idea how to make the next thing

>>> n=4 #split into so long parts
>>> l = (1,2,3,4,5,1,2,3,4,5,1,2,3,4,5) #this is the tuple to split
>>> [l[i:i+n] for i in range(0,len(l),n)]
[(1, 2, 3, 4), (5, 1, 2, 3), (4, 5, 1, 2), (3, 4, 5)]

But I have to make it like this
[(1, 2, 3, 4), (5, 1, 2, 3), (4, 5, 1, 2), (3, 4, 5, default)]
because i use it later in this

>>> result = [l[i:i+n] for i in range(0,len(l),n)]
>>> zip(*result)
[(1, 5, 4, 3), (2, 1, 5, 4), (3, 2, 1, 5)]

and as you can see it, the last element is missing here.



Yours sincerely,
______________________________
János Juhász



More information about the Tutor mailing list