Transform 1D List
<p>I'm new to python and I need to transform a list of number(0,1,2,3) into a 2D array. Basically, they both have the same row number however the value in the 1D list indicates it's column number in the 2D array. The value in the 2D list is marked with a 1. For example, a sample 1D list </p>

<pre><code>list_1d = [1,0,3,2,0]

1
0
3
2
0
</code></pre>

<p>I need it to be transformed to, </p>

<pre><code>0 1 0 0
1 0 0 0
0 0 1 0
0 1 0 0
1 0 0 0
</code></pre>

<p>I have created a 2D array of zeros, </p>

<pre><code>array_2d = np.zeros((len(list_1d),4))
</code></pre>

<p>Kindly appreciated. </p>

<p>Here's a one liner</p>

<pre><code>&gt;&gt;&gt; np.array([[ 1 if i == n else 0 for i in range(4)] for n in list_1d])
array([[0, 1, 0, 0],
       [1, 0, 0, 0],
       [0, 0, 0, 1],
       [0, 0, 1, 0],
       [1, 0, 0, 0]])
</code></pre>

<p>It uses a nested couple of comprehensions to do what you need.  The width of the array is hardwired (the <code>range(4)</code>)</p>

python arrays list numpy transform