Better way to swap elements in a list?
<p>I have a bunch of lists that look like this one:</p>

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

<p>I want to swap elements as follows:</p>

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

<p>The size of the lists may vary, but they will always contain an even number of elements.</p>

<p>I'm fairly new to Python and am currently doing it like this:</p>

<pre><code>l =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
final_l = []
for i in range(0, len(l)/2):
    final_l.append(l[2*i+1])
    final_l.append(l[2*i])
</code></pre>

<p>I know this isn't really <a href="https://en.wiktionary.org/wiki/Pythonic#Adjective">Pythonic</a> and would like to use something more efficient. Maybe a list comprehension?</p>

<p>No need for complicated logic, simply rearrange the list with slicing and step:</p>

<pre><code>In [1]: l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In [2]: l[::2], l[1::2] = l[1::2], l[::2]

In [3]: l
Out[3]: [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]
</code></pre>

<hr>

<h2>Â TLDR;</h2>

<p><strong>Edited with explanation</strong></p>

<p>I believe most viewers are already familiar with list slicing and multiple assignment. In case you don't I will try my best to explain what's going on (hope I do not make it worse).</p>

<p>To understand list slicing, <a href="http://stackoverflow.com/questions/509211/explain-pythons-slice-notation">here</a> already has an excellent answer and explanation of list slice notation.
Simply put:</p>

<pre><code>a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:end:step] # start through not past end, by step
</code></pre>

<p>Let's look at OP's requirements: </p>

<pre><code> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # list l
  ^  ^  ^  ^  ^  ^  ^  ^  ^  ^
  0  1  2  3  4  5  6  7  8  9    # respective index of the elements
l[0]  l[2]  l[4]  l[6]  l[8]      # first tier : start=0, step=2
   l[1]  l[3]  l[5]  l[7]  l[9]   # second tier: start=1, step=2
-----------------------------------------------------------------------
l[1]  l[3]  l[5]  l[7]  l[9]
   l[0]  l[2]  l[4]  l[6]  l[8]   # desired output
</code></pre>

<p>First tier will be: <code>l[::2] = [1, 3, 5, 7, 9]</code>
Second tier will be: <code>l[1::2] = [2, 4, 6, 8, 10]</code></p>

<p>As we want to re-assign <code>first = second</code> &amp; <code>second = first</code>, we can use multiple assignment, and update the original list in place:</p>

<pre><code>first , second  = second , first
</code></pre>

<p>that is:</p>

<pre><code>l[::2], l[1::2] = l[1::2], l[::2]
</code></pre>

<p>As a side note, to get a new list but not altering original <code>l</code>, we can assign a new list from <code>l</code>, and perform above, that is:</p>

<pre><code>n = l[:]  # assign n as a copy of l (without [:], n still points to l)
n[::2], n[1::2] = n[1::2], n[::2]
</code></pre>

<p>Hopefully I do not confuse any of you with this added explanation. If it does, please help update mine and make it better :-) </p>

python