Find Max in List Using the Reduce Function
<p>In TorbjÃ¶rn Lager's 46 python exercises, number 26 is finding the max in a list using the reduce function. I know how to add and multiply using the reduce function but  it doesn't make sense to me how you could use it to find the largest number. Does anyone know how to do this?</p>

<p>Write a function that returns the larger of two numbers:</p>

<pre><code>def larger(a, b):
    return a if a &gt; b else b
</code></pre>

<p>Then use it with <code>reduce</code>:</p>

<pre><code>reduce(larger, [1, 2, 3, 4])
</code></pre>

<p>Conveniently, Python already has a function like <code>larger</code> that's called <code>max</code>. So this will work:</p>

<pre><code>reduce(max, [1, 2, 3, 4])
</code></pre>

<p>Of course <code>max</code> will do the whole kaboodle for you without <code>reduce</code>:</p>

<pre><code>max([1, 2, 3, 4])
</code></pre>

<p>And that's what you'd actually use if you weren't learning to work with <code>reduce</code>. It doesn't use <code>reduce</code> under the hood, though; that's actually a rather inefficient way to do the job.</p>

python