On 21/11/05, János Juhász <[EMAIL PROTECTED]> wrote: > I can't imagine how this could be made with list comprehension. > > >>> import operator > >>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) > >>> reduce(operator.add, a) # it makes a long list now > ([1], [2], [3, 31, 32], [4], [5], [6], [7, 71, 72], [8], [9])
Everything is possible with list comprehensions! >>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) >>> [x for y in a for x in y] [[1], [2], [3, 31, 32], [4], [5], [6], [7, 71, 72], [8], [9]] We can even go a level deeper! >>> [x for z in a for y in z for x in y] [1, 2, 3, 31, 32, 4, 5, 6, 7, 71, 72, 8, 9] Just make sure your depth is consistent throughout. And remember that that single-line expression is hiding nested FOR loops! -- John. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor