Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Looping over tuple is slightly faster than looping over list

Looping over a tuple is faster than looping over a list in Python2

In Python2:

In [1]: def loop_list():    
   ...:     for i in [1, 2, 3]:                          
   ...:         pass        
   ...:                     

In [2]: def loop_tuple():   
   ...:     for i in (1, 2, 3):                          
   ...:         pass        
   ...:                     

In [3]: import dis          

In [4]: dis.dis(loop_list)  
  2           0 SETUP_LOOP              23 (to 26)       
              3 LOAD_CONST               1 (1)           
              6 LOAD_CONST               2 (2)           
              9 LOAD_CONST               3 (3)           
             12 BUILD_LIST               3               
             15 GET_ITER                                 
        >>   16 FOR_ITER                 6 (to 25)       
             19 STORE_FAST               0 (i)           

  3          22 JUMP_ABSOLUTE           16               
        >>   25 POP_BLOCK                                
        >>   26 LOAD_CONST               0 (None)        
             29 RETURN_VALUE                             

In [5]: dis.dis(loop_tuple) 
  2           0 SETUP_LOOP              14 (to 17)       
              3 LOAD_CONST               4 ((1, 2, 3))   
              6 GET_ITER                                 
        >>    7 FOR_ITER                 6 (to 16)       
             10 STORE_FAST               0 (i)           

  3          13 JUMP_ABSOLUTE            7               
        >>   16 POP_BLOCK                                
        >>   17 LOAD_CONST               0 (None)        
             20 RETURN_VALUE

In Python3:

In [1]: def loop_list():    
   ...:     for i in [1, 2, 3]:                          
   ...:         pass        
   ...:                     

In [2]: def loop_tuple():   
   ...:     for i in (1, 2, 3):                          
   ...:         pass        
   ...:                     

In [3]: import dis          

In [4]: dis.dis(loop_list)  
  2           0 SETUP_LOOP              12 (to 14)       
              2 LOAD_CONST               4 ((1, 2, 3))   
              4 GET_ITER    
        >>    6 FOR_ITER                 4 (to 12)       
              8 STORE_FAST               0 (i)           

  3          10 JUMP_ABSOLUTE            6               
        >>   12 POP_BLOCK   
        >>   14 LOAD_CONST               0 (None)        
             16 RETURN_VALUE                             

In [5]: dis.dis(loop_tuple) 
  2           0 SETUP_LOOP              12 (to 14)       
              2 LOAD_CONST               4 ((1, 2, 3))   
              4 GET_ITER    
        >>    6 FOR_ITER                 4 (to 12)       
              8 STORE_FAST               0 (i)           

  3          10 JUMP_ABSOLUTE            6               
        >>   12 POP_BLOCK   
        >>   14 LOAD_CONST               0 (None)        
             16 RETURN_VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.