Pretty Printing (2D Arrays, Box)
<p>I have written the following code:</p>

<pre><code>for row in range(len(listOfLists)):
    print('+' + '-+'*len(listOfLists))
    print('|', end='')
    for col in range(len(listOfLists[row])):
        print(listOfLists[row][col], end='|')
    print(' ') #To change lines 
print('+' + '-+'*len(listOfLists))
</code></pre>

<p>Input: </p>

<blockquote>
<pre><code>[['a', 'b', 'c'],
 ['d', 'e', 'f'],
 ['g', 'h', 'i'],
 ['j', 'k', 'l']]
</code></pre>
</blockquote>

<p>Output:</p>

<blockquote>
<pre><code>+-+-+-+-+
|a|b|c| 
+-+-+-+-+
|d|e|f| 
+-+-+-+-+
|g|h|i| 
+-+-+-+-+
|j|k|l| 
+-+-+-+-+
</code></pre>
</blockquote>

<p>Desired Output:</p>

<blockquote>
<pre><code>+-+-+-+
|a|b|c| 
+-+-+-+
|d|e|f| 
+-+-+-+
|g|h|i| 
+-+-+-+
|j|k|l| 
+-+-+-+
</code></pre>
</blockquote>

<p>Which prints a '+-+' around the elements of the 2D array.
However, my code only works for a square array (n^2).</p>

<p>How can I generalise it so that it works for any variation of array (as long as all lists are equal length)</p>

<p>Thank you</p>

<p>You are printing this separator based on number of rows, not number of columns. Using additional test cases helps with debugging immediately.</p>

<pre><code>def printListOfLists(listOfLists):
    for row in range(len(listOfLists)):
        print('+' + '-+' * len(listOfLists[0]))
        print('|', end='')
        for col in range(len(listOfLists[row])):
            print(listOfLists[row][col], end='|')
        print(' ')  # To change lines
    print('+' + '-+' * len(listOfLists[0]))


printListOfLists([
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['g', 'h', 'i'],
    ['j', 'k', 'l']
])

printListOfLists([['a', 'b', 'c']])

printListOfLists([['a'], ['b'], ['c']])
</code></pre>

<p>All produced results are now expected:</p>

<pre><code>+-+-+-+
|a|b|c| 
+-+-+-+
|d|e|f| 
+-+-+-+
|g|h|i| 
+-+-+-+
|j|k|l| 
+-+-+-+


+-+-+-+
|a|b|c| 
+-+-+-+


+-+
|a| 
+-+
|b| 
+-+
|c| 
+-+
</code></pre>

python arrays multidimensional-array