[Python-ideas] Tuple Comprehensions

Ben Finney ben+python at benfinney.id.au
Sun Oct 9 23:21:19 CEST 2011


Karthick Sankarachary
<karthick.sankarachary at gmail.com> writes:

> To address both of the above, I'd like to introduce tuple
> comprehensions, which would work like so:
>
> >>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
> >>> (weapon.strip() for weapon in freshfruit)
> ('banana', 'loganberry', 'passion fruit')
> >>> import operator
> >>> (operator.concat(_, weapon.strip()) for weapon in freshfruit)
> 'bananaloganberrypassion fruit'
>
> As you can see, we use parenthesis instead of square brackets around
> the comprehension.

That's already valid syntax. The parentheses are used around generator
expressions in existing code.

Since that's the case, why not just:

    tuple(weapon.strip() for weapon in freshfruit)

if you want the generator expression turned into a tuple?

-- 
 \     “When people believe that they have absolute knowledge, with no |
  `\     test in reality, this [the Auschwitz crematorium] is how they |
_o__)             behave.” —Jacob Bronowski, _The Ascent of Man_, 1973 |
Ben Finney




More information about the Python-ideas mailing list