Convert a 1D list to 2D at every "\n" - Python
<p>I am trying to convert a 1D list to 2D whenever there is <code>\n</code> in it. </p>

<pre><code>a = ['a', 'b', '\n', 'c', 'd']
</code></pre>

<p>to</p>

<pre><code>a = [['a', 'b']['c', 'd']]
</code></pre>

<p>Can anyone help me?</p>

<p><strong>Edit</strong></p>

<p>This is because I have a string</p>

<pre><code>a = """\
f:         1000:000 000 000 000 000 000 000 000 000 000 000 000 f:         2000:000 000 000 000 000 000 000 000 000 000 000 000
f:         0000:000 000 000 000 000 000 000 000 000 000 000 000 f:         0000:000 000 000 000 000 000 000 000 000 000 000 000
f:         0000:000 000 000 000 000 000 000 666 000 000 000 000 f:         0000:000 000 000 000 000 000 000 000 000 000 000 000
f:         0000:000 000 000 000 000 333 000 000 000 000 000 000 f:         0000:000 000 000 000 000 000 000 000 000 000 000 000
"""
</code></pre>

<p>So when I convert it into listby doing <code>list(a)</code>, at every line break I get <code>\n</code>, so I just want to split it there.</p>

<p>Simplest way is to join the list to make string. Then split it on '\n' and convert the each string in the list to sub list:</p>

<pre><code>&gt;&gt;&gt; a_string = ''.join(a)
&gt;&gt;&gt; map(list, a_string.split())
[['a', 'b'], ['c', 'd']]
</code></pre>

<p>However if you do not want to go with this approach, below is the simple logic based on <code>for</code> loop:</p>

<pre><code>&gt;&gt;&gt; my_list = []
&gt;&gt;&gt; sub_list = []
&gt;&gt;&gt; for item in a:
...     if item != '\n':
...         sub_list.append(item)
...     else:
...         my_list.append(sub_list)
...         sub_list = []
...
&gt;&gt;&gt; my_list.append(sub_list)
&gt;&gt;&gt; my_list
[['a', 'b'], ['c', 'd']]
</code></pre>

python