Confusion with comparison error
<p>When i run the following </p>

<pre class="lang-python prettyprint-override"><code>def max(L):
    m = L[0][0]
    for item in L:
        if item[0] &gt; m:
            m = item
    return m

L = [[20, 10], [10, 20], [30, 20],[12,16]]
print(max(L))
</code></pre>

<p>i get the error 
 <code>TypeError: unorderable types: int() &gt; list()</code> at line 4. The confusion comes when i try to get the <code>len()</code> of both members. So from the error message it's reasonable to assume <code>m</code> is the list so i run </p>

<pre class="lang-python prettyprint-override"><code>def max(L):
    m = L[0][0]
    for item in L:
        len(m)
        if item[0] &gt; m:
            m = item
    return m

L = [[20, 10], [10, 20], [30, 20],[12,16]]
print(max(L))
</code></pre>

<p>and get the error <code>len(m) TypeError: object of type 'int' has no len()</code>. Ok so the only option left is that <code>item[0]</code> is the list... so similarly </p>

<pre class="lang-python prettyprint-override"><code>def max(L):
    m = L[0][0]
    for item in L:
        len(item[0])
        if item[0] &gt; m:
            m = item
    return m

L = [[20, 10], [10, 20], [30, 20],[12,16]]
print(max(L))
</code></pre>

<p>and i get the same error: <code>len(item[0]) TypeError: object of type 'int' has no len()</code>. Since I'm somewhat certain you can compare 2 ints, I have a hard time understanding what to do about the error originally stated.</p>

<p>As Moses Koledoye says, you are getting that <code>TypeError: unorderable types: int() &gt; list()</code> error because the first assignment in the loop assigns the whole <code>item</code> to <code>m</code>, so the next time you attempt to compare you're comparing the list <code>m</code> with the integer <code>item[0]</code>. So you just need to assign <code>item[0]</code> to <code>m</code>. Like this:</p>

<pre><code>def max0(L):
    m = L[0][0]
    for item in L:
        if item[0] &gt; m:
            m = item[0]
    return m

L = [[20, 10], [10, 20], [30, 20], [12, 16]]
print(max0(L))
</code></pre>

<p><strong>output</strong></p>

<pre><code>30
</code></pre>

<p>But there's a better way to do this: use the built-in <code>max</code> with a key function that grabs the first element from each list in the sequence you pass to `max.</p>

<pre><code>from operator import itemgetter

L = [[20, 10], [10, 20], [30, 20], [12, 16]]
m = max(L, key=itemgetter(0))
print(m)
</code></pre>

<p><strong>output</strong></p>

<pre><code>[30, 20]
</code></pre>

<p>You can also do this with a simple <code>lambda</code> function, rather than importing <code>itemgetter</code>, but <code>itemgetter</code> is more efficient.</p>

<pre><code>m = max(L, key=lambda u:u[0])
</code></pre>

<p>In fact, you don't really need to supply a key function here because Python will happily compare 2 lists (or tuples). It compares the corresponding elements in the two lists, stopping as soon as it finds a pair of elements that are unequal. So <code>[30, 20] &gt; [30, 19]</code> evaluates to <code>True</code>, and so does <code>[30, 20] &gt; [29, 1000]</code>. The 2 lists don't have to be the same length; <code>[30, 20, 0] &gt; [30, 20]</code> evaluates to <code>True</code>.</p>

<p>So you could just do </p>

<pre><code>m = max(L)
</code></pre>

<p>but using <code>itemgetter</code> is better (and probably more efficient) because it explicitly says to only compare the sublists by their first element.</p>

python list int comparison