Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to swap elements in Python list

Is there any any faster way to swap two list elements in Python than

L[a], L[b] = L[b], L[a] 

or would I have to resort to Cython or Weave or the like?

like image 572
Gerald Senarclens de Grancy Avatar asked Dec 29 '10 12:12

Gerald Senarclens de Grancy


People also ask

Can we swap the elements of list in Python?

Given a list in Python and provided the positions of the elements, write a program to swap the two elements in the list. Since the positions of the elements are known, we can simply swap the positions of the elements.

How do you switch multiple items in a list in Python?

To swap two list elements by index i and j , use the multiple assignment expression lst[i], lst[j] = lst[j], lst[i] that assigns the element at index i to index j and vice versa.

How do you swap two elements?

The standard solution to swap two elements in a List is using the Collections. swap() method, which swaps the elements at the specified positions in a list.


2 Answers

Looks like the Python compiler optimizes out the temporary tuple with this construct:

code:

import dis  def swap1():   a=5   b=4   a, b = b, a  def swap2():   a=5   b=4   c = a   a = b   b = c  print 'swap1():' dis.dis(swap1) print 'swap2():' dis.dis(swap2) 

output:

swap1():   6           0 LOAD_CONST               1 (5)               3 STORE_FAST               0 (a)    7           6 LOAD_CONST               2 (4)               9 STORE_FAST               1 (b)    8          12 LOAD_FAST                1 (b)              15 LOAD_FAST                0 (a)              18 ROT_TWO                           19 STORE_FAST               0 (a)              22 STORE_FAST               1 (b)              25 LOAD_CONST               0 (None)              28 RETURN_VALUE         swap2():  11           0 LOAD_CONST               1 (5)               3 STORE_FAST               0 (a)   12           6 LOAD_CONST               2 (4)               9 STORE_FAST               1 (b)   13          12 LOAD_FAST                0 (a)              15 STORE_FAST               2 (c)   14          18 LOAD_FAST                1 (b)              21 STORE_FAST               0 (a)   15          24 LOAD_FAST                2 (c)              27 STORE_FAST               1 (b)              30 LOAD_CONST               0 (None)              33 RETURN_VALUE         

Two loads, a ROT_TWO, and two saves, versus three loads and three saves. You are unlikely to find a faster mechanism.

like image 163
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 05:09

Ignacio Vazquez-Abrams


If you could post a representative code sample, we could do a better job of benchmarking your options. FWIW, for the following dumb benchmark, I get about a 3x speedup with Shed Skin and a 10x speedup with PyPy.

from time import time  def swap(L):     for i in xrange(1000000):         for b, a in enumerate(L):             L[a], L[b] = L[b], L[a]  def main():     start = time()     L = list(reversed(range(100)))     swap(L[:])     print time() - start     return L  if __name__ == "__main__":     print len(main())  # for shedskin: # shedskin -b -r -e listswap.py && make # python -c "import listswap; print len(listswap.main())" 
like image 27
TryPyPy Avatar answered Sep 23 '22 05:09

TryPyPy