>>> 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])

When I make list comprehension, the list hierarchy is allways the same or

>>> [item for item in a] # the deepnes is the same
[([1], [2], [3, 31, 32], [4]), ([5], [6], [7, 71, 72]), ([8], [9])]
>>> [(item, item) for item in a] # it is deeper with one level

You are not using operator add anywhere in there. 
So you won't reduce anything!

But reduce is tricky to reproduce using comprehensions because 
it involves combining two elements using an operation. And one 
of the elements is the running result.

So

count = 0
[count + item for item in alist]

won't work because we never change count - and assignments 
aren't allowed inside a comprehension.

There may be a clever way of doing it but I can't think of it and 
certainly not a way that would be as readable as reduce()!

Do you have any particular need to avoid reduce? Or is it just curiosity?

Alan g


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to