Convert a string into list of list
<p>I have a string, say:</p>

<p><code>s = "abcdefghi"</code></p>

<p>I need to create a list of lists of this such that the list formed will :-</p>

<p><code>list = [['a','b','c'],['d','e','f'],['g','h','i']]</code></p>

<p>Now the string can be anything but will always have length n * n</p>

<p>So if <code>n = 4.</code></p>

<p>The <code>len(s) = 16</code></p>

<p>And the list will be:</p>

<pre><code>[['1','2','3','4'],['5','6','7','8'],...,['13','14','15','16']]
</code></pre>

<p>So each list in the list of lists will have length 4.</p>

<p>So I want to write a function which takes string and n as input and gives 
a list of list as output where the length of each list within the list is n.</p>

<p>How can one do this ?</p>

<p>UPDATE :</p>

<p>How can you convert the above list of list back to a string ?</p>

<p>So if l = <code>list = [['a','b','c'],['d','e','f'],['g','h','i']]</code></p>

<p>How to convert it to a string :-</p>

<p><code>s = "abcdefghi"</code></p>

<p>I found a solution for this in stackoverflow :-</p>

<pre><code>s = "".join("".join(map(str,l)) for l in list)
</code></pre>

<p>Thanks everyone !</p>

<p>I'm a personal fan of numpy...</p>

<pre><code>import numpy as np
s = "abcdefghi"
n = int(np.sqrt(len(s)))
a = np.array([x for x in s], dtype=str).reshape([n,n])
</code></pre>

python string list