Given a list/tuple split original list based on either one of two parameters given but NOT both,
num_fractions : Number of subsets original list has to be divided into, of same size to the extent possible.
In case equilength subsets can't be generated, all but the last subset
will have the same number of elements.
subset_length : Split on every subset_length elements till the list is exhausted.
if not input_list:
yield input_list #For some reason I can't just return from here : return not allowed in generator expression
elif not bool(num_fractions) ^ bool(subset_length): #Will check for both the invalid cases, '0' and 'None'.. Oh Python :)
raise Exception("Only one of the params : num_fractions,subset_length to be provided")
if num_fractions: #calcluate subset_length in this case
subset_length = max(len(input_list)/num_fractions,1)
for start in xrange(0,len(input_list),subset_length):
>> list(list_split.split((2, 2, 10, 10, 344, 344, 45, 43, 2, 2, 10, 10, 12, 8, 2, 10),subset_length=4))
[(2, 2, 10, 10), (344, 344, 45, 43), (2, 2, 10, 10), (12, 8, 2, 10)]
The code is longer than the solutions given above, but covers all possible sequence split conditions.
Thanks but the one above works just fine. saving the code as it could be very use full in the right place.