title: Best way to write Python 2 and 3 compatible code using nothing but the standard library
body: <p>I am trying to make some of my code Python 2 and 3 compatible.</p>
  
  <p>At the moment I am struggling with functions like <code>range</code>/<code>xrange</code> and methods like <code>dict.items</code>/<code>dict.iteritems</code>. Ideally I would like my code to be able to use the former in Python 3.x and the latter in Python 2.x.</p>
  
  <p>Using <code>if</code>/<code>else</code> seems to me to be the easiest way to implement this:</p>
  
  <pre><code>if py &gt;= 3:
      for item in array.items()
      ...
  else:
      for item in array.iteritems()
  </code></pre>
  
  <p>However, doing like that results in lots of repeated and ugly code. Is there a better way to do that <strong>using only the standard library</strong>? Can I just state somewhere at the beginning of the code to <em>always</em> use <code>range</code>/<code>dict.items</code> if <code>py &gt;= 3</code> and <code>xrange</code>/<code>dict.iteritems</code> if not?</p>
  
  <p>Is it possible to do something like this?</p>
  
  <pre><code>if py &lt; 3:
      use xrange as range
  </code></pre>
  
  <p>I have looked around and I know that several libraries, like six o futurize) are used to solve this issue. However I am working on a server that run only python 2.7 and I am not allowed to install any extra libraries on it. I have some python3 code I would like to use but I also want to maintain only one version of the code.</p>