Equating the lengths of the arrays in an array of arrays
<p>Given an array of arrays with different lengths. Is there a cleaner (shorter) way to equate the lengths of the arrays by filling the shorter ones with zeros other than the following code:</p>

<pre><code>a = [[1.0, 2.0, 3.0, 4.0],[2.0, 3.0, 1.0],[5.0, 5.0, 5.0, 5.0],[1.0, 1.0]]


max =0
for x in a:
    if len(x) &gt; max:
        max = len(x)

print max
new = []
for x in a:
    if len(x)&lt;max:
        x.extend([0.0]* (max-len(x)) )
    new.append(x)

print new
</code></pre>

<p>You can find the length of the largest list within <code>a</code> using either:</p>

<p><code>len(max(a, key=len))</code> </p>

<p>or </p>

<p><code>max(map(len, a))</code></p>

<p>and also use a list comprehension to construct a new list:</p>

<pre><code>&gt;&gt;&gt; a = [[1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 1.0], [5.0, 5.0, 5.0, 5.0], [1.0, 1.0]]
&gt;&gt;&gt; m = len(max(a, key=len))
&gt;&gt;&gt; new = [x + [0]*(m - len(x)) for x in a]
&gt;&gt;&gt; new
[[1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 1.0, 0], [5.0, 5.0, 5.0, 5.0], [1.0, 1.0, 0, 0]]
</code></pre>

python