Python: sorting sublist values in nested list
<p>I have a nested list:
<code>list = [[3,2,1],[8,7,9],[5,6,4]]</code></p>

<p>I want to sort each sublist in ascending order. My though was to loop through the list, but it only wants to sort the last sublist.</p>

<p><code>for i in list:
    newList = sorted[i]</code></p>

<p>So for this example the for loop would loop through the whole list, but only sort the last sublist [5,6,4]. How do I make it sort all the sublists?</p>

<p>Python version: 2.7.10 </p>

<p>OS: OS X El Capitan</p>

<p>You are creating a new list and assigning it to a new name, then throwing it away. Use the <code>sort</code> method to sort each list reference to in-place. Also note that it doesn't make sense to use a list reference to index the main list, and that you should avoid naming variables with the same name as built-in functions (<code>list</code> in this case).</p>

<pre><code>&gt;&gt;&gt; l = [[3,2,1],[8,7,9],[5,6,4]]
&gt;&gt;&gt; for i in l:
...     i.sort()
...
&gt;&gt;&gt; l
[[1, 2, 3], [7, 8, 9], [4, 5, 6]]
</code></pre>

python python-2.7