max() of big nested lists
<p>I encountered a quite odd problem while dealing with the max of a long list of lists of of pairs, such as</p>

<pre><code>[
    [(0, 1), (1, 1), (2, 1), (3, 4), (4, 1), (5, 1), (6, 1),...,(141,3)],
    ..., 
    [(12, 1), (36, 1), (91, 1), (92, 1), (110, 1),..., (180, 1)]
]
</code></pre>

<p>I am trying to get the maximum of the first element of all the pairs.
Pythonically, I was doing:</p>

<pre><code>max([max(x) for x in list])[0]
</code></pre>

<p>which actually returns the correct number, IF the list is shorter than 281 lists.
In fact, as soon as the list is longer than 280, I get this message</p>

<pre><code>ValueError: max() arg is an empty sequence
</code></pre>

<p>So, for a long list</p>

<pre><code>max([max(x) for x in list[0:280]])[0]
</code></pre>

<p>it's fine, while</p>

<pre><code>max([max(x) for x in list[0:281]])[0]
</code></pre>

<p>breaks.</p>

<p>Am I doing something wrong here?</p>

<p>Why not just this?</p>

<pre><code>max([max([t[0] for t in sl if t]) for sl in l if sl])
</code></pre>

<p>You can extract the first item from the beginning.  Empty lists and tuples are ignored.</p>

<p>EDIT</p>

<pre><code>max([max([t for t in sl if t]) for sl in l if sl])[0]
</code></pre>

<p>is more efficient.</p>

python list max nested-lists