Appending a list into a list of lists in python
<p>I am trying to create and append a list and index in a list. Appending any list element is being automatically appended to all the lists available in this list
First of all I have a list as following</p>

<pre><code>sygma_list [[]] * 3
</code></pre>

<p>and I have another lists having the form</p>

<pre><code>mts_columns1 = [[1,2,3], [4,5,6], [6,7,8]]
mts_columns2 = [[1,2,3], [4,5,6], [6,7,8]]
</code></pre>

<p>When looping over the sygma_list I have like so:</p>

<pre><code> for i in range(0, 3):
       sygma_list[i].append(mts_column[i])
</code></pre>

<p>the results of sygma_list are being quite shocking, as append() is behaving on each element of the list instead of obtaining a final result of</p>

<pre><code>sygma_list = [[[1,2,3], [1,2,3]],
              [[4,5,6],[4,5,6]],
              [[6,7,8],[6,7,8]]]
</code></pre>

<p>If all the <code>mts_columns</code> lists contain the same sublists as in your post
then you could use a list comprehension like so:</p>

<pre><code>sygma_list = [ [i]*2 for i in mts_columns]
</code></pre>

<p>in this case <code>sygma_list</code> doesn't have to be initialized as in your post (or you could simply do <code>sygma_list = []</code>, but you don't have to ) as it will be created automatically, and will give you the following output:</p>

<pre><code>[[[1, 2, 3], [1, 2, 3]], 
 [[4, 5, 6], [4, 5, 6]], 
 [[6, 7, 8], [6, 7, 8]]]
</code></pre>

<p>Of course you can change <strong>2</strong> to whatever number you like (2 is the number of <code>mts_columns</code> we choose for the creation of <code>sygma_list</code>).</p>

<p>Now if the <code>mts_columns</code> lists do not contain the same sublilsts, I mean the case that you would have <em>mts_columns1, mts_columns2,...</em> lists and each of those lists contained different sublists from the other,
then you could also use a list comprehension like:</p>

<pre><code>sygma_list = [ [i,j] for i,j in zip(mts_columns1,mts_columns2)]
</code></pre>

python list append