How to reverse the elements in a sublist?
<p>I'm trying to create a function that reverses the order of the elements in a list, and also reverses the elements in a sublist. for example:</p>

<p>For example, if L = [[1, 2], [3, 4], [5, 6, 7]] then deep_reverse(L) mutates L to be [[7, 6, 5], [4, 3], [2, 1]]</p>

<p>I figured out how to reverse the order of one list, but I am having troubles with reversing the order of elements in a sublist. This is what I have so far:</p>

<pre><code>def deep_reverse(L)
    """ 
    assumes L is a list of lists whose elements are ints
    Mutates L such that it reverses its elements and also 
    reverses the order of the int elements in every element of L. 
    It does not return anything.
    """
    for i in reversed(L):
          print(i)
</code></pre>

<p>In the example above, my code would just print <code>[5,6,7], [3,4], [1,2]</code>, which is not what i'm trying to accomplish. It's just reversing the order of the lists, the not actual elements in the lists.</p>

<p>What should I add to the code so that it also reverses the order of the elements in a sublist?</p>

<p>[<strong>EDIT</strong>: my code <strong>needs</strong> to mutate the list; I don't want it just to print it, it actually needs to change the list.]</p>

<p>This looks very familiar :). I'm not going to give the whole working solution  but here are some tips:</p>

<p>As you know, there are two steps, reverse each sub-list and then reverse the outer list (in place, without making a new list, so it will mutate the global <code>L</code>).</p>

<p>So you can loop through the outer list, and mutate each sub-list:</p>

<pre><code>for i in range(len(L)):
    # if L[i] is a list:
        # reverse with [::-1] and update L[i] to the reversed version
# reverse the outer list L, list.reverse() will operate in-place on L
</code></pre>

<p>Now remember, if you loop through the list like this:</p>

<pre><code>for item in list:
    item = 'xxx'
</code></pre>

<p>You can't change <code>item</code> with the above code. <a class='doc-link' href="http://stackoverflow.com/documentation/python/3553/common-pitfalls/949/changing-the-sequence-you-are-iterating-over#t=201610022012362266711"><code>item</code> is a placeholder value, so changing it doesn't actually modify the list</a>. </p>

<p>You instead need to index the item in <code>L</code>, and enumerate can help with this, or you can use the less-preffered <code>range(len())</code> as above.</p>

<pre><code>for i, item in enumerate(L):
    # do something with L[i]
    L[i] = 'something'
</code></pre>

<p><strong>Edit: since there is so much confusion over this, I'll go ahead and post a working solution based on Stefan Pochmann's very elegant answer:</strong></p>

<pre><code>def deep_reverse(L):
    L.reverse()
    for sublist in L:
        sublist.reverse()
</code></pre>

<p>Notice there is <strong>no return statement, and no print statement</strong>. This will correctly modify <code>L</code> in place. You <strong>cannot</strong> reassign <code>L</code> inside the function because then it will just create a new local version of <code>L</code>, and it will not modify the global <code>L</code>. You can use <code>list.reverse()</code> to modify <code>L</code> <strong>in place</strong> which is necessary based on the specifications. </p>

python list python-3.x reverse sublist