[Python-ideas] Tuple Comprehensions

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Tue Oct 11 02:42:03 CEST 2011


On Oct 10, 2011, at 1:36 PM, Karthick Sankarachary wrote:

> Hi, All,
> 
> As Masklinn pointed out, the issue is that there's no concise way to accumulate or compress an iterable without resorting to the use of the reduce() function.
> 
> To answer Greg's point, it's not like list comprehensions cover all the cases allowed by the concept of mapping. There are cases, such as when the construction rule is too complicated, where comprehensions cannot be applied. Given that, why not explore the idea of if and how we can make (simple) reductions more readable.

…snip…

> Here's another example to illustrate the desired behavior, which basically adds a list of numbers (without resorting to the use of a reduce()):
> 
> >>> numbers=[1,2,3,4,5]
> >>> [_+number for number in numbers]
> 15 

Here's an even more readable version:

> >>> numbers=[1,2,3,4,5]
> >>> total = sum(numbers)

I think the simple reductions (addition, concatenation) are already well covered by existing Python syntax. There's no need to try make sugar for anything more complicated than those two. Use a loop!

-- Carl 


More information about the Python-ideas mailing list