> for example: > a = [1,2,3,4,5,6,7,8,9,10] #Although not necessarily in order > > def cmp(i,j): #to be defined in this thread.
Well, if you're willing to give up doing it in a cmp() method, you can do it as such: >>> a.sort() >>> chunk_size = 3 >>> [a[i::chunk_size] for i in range(chunk_size)] [[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]] If you need it in a flat list, rather than as a list of chunk_size lists (which are handy for iterating over in many cases), there are ways of obtaining it, such as the hackish >>> sum([a[i::chunk_size] for i in range(chunk_size)], []) [1, 4, 7, 10, 2, 5, 8, 3, 6, 9] There are likely good recipes for flattening a list. I just happen not to have any at my fingertips. I'm not sure it's possible to do in a cmp() method, given that it requires apriori knowledge of the dataset (are the numbers contiguous?). Unless, of course, you have such a list... However, as a benefit, this method should work no matter what the list contains, as long as they're comparable to each other for an initial sorting: >>> a = [chr(ord('a') + i) for i in range(10)] >>> # a.sort() if it were needed >>> a ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] >>> sum([a[i::chunk_size] for i in range(chunk_size)], []) ['a', 'd', 'g', 'j', 'b', 'e', 'h', 'c', 'f', 'i'] -tkc -- http://mail.python.org/mailman/listinfo/python-list