Hello Python Ideas,<br><br>To quote the tutorial, "List comprehensions provide a concise way to create lists without resorting to use of map(), filter() and/or lambda.".<br><br>There are a couple of constraints in the above definition that I feel could be overcome. For one thing, list comprehensions don't let you create immutable sequences (such as a tuple) by design. For another, it does not provide a concise alternative to reduce() function.<br>
<br>To address both of the above, I'd like to introduce tuple comprehensions, which would work like so:<br><br><pre><span class="gp">>>> </span><span class="n">freshfruit</span> <span class="o">=</span> <span class="p">[</span><span class="s">' banana'</span><span class="p">,</span> <span class="s">' loganberry '</span><span class="p">,</span> <span class="s">'passion fruit '</span><span class="p">]<br>
</span><span class="gp">>>> </span><span class="p">(</span><span class="n">weapon</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">weapon</span> <span class="ow">in</span> <span class="n">freshfruit</span><span class="p">)</span><br>
<span class="go">('banana', 'loganberry', 'passion fruit')<br>>>> import operator<br>>>> (operator.concat(_, weapon.strip()) for weapon in freshfruit)<br>'bananaloganberrypassion fruit'<br>
<br></span></pre>As you can see, we use parenthesis instead of square brackets around the comprehension. <br><br>In the first tuple comprehension, we create a true tuple with multiple items. This might a tad more efficient, not to mention less verbose, than applying the "tuple" function on top of a list comprehension.<br>
<br>In the second tuple comprehension, we use a reduce() function (specifically operator.concat) to concatenate all of the fruit names. In particular, we use the "_" variable (for lack of a better name) to track the running outcome of the reduce() function.<br>
<br>Best Regards,<br>Karthick Sankarachary<br>