<story><title>Python idioms</title><url>http://jaynes.colorado.edu/PythonIdioms.html</url><text></text></story><parent_chain></parent_chain><comment><author>Estragon</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;  &amp;#62; [Despite the existence of &quot;enumerate&quot;, &quot;xrange(maxint)&quot;] can still
  &amp;#62; be useful when you want to include an index along with several
  &amp;#62; other lists, however, e.g. zip(list_1, list_2, indices)
&lt;/code&gt;&lt;/pre&gt;
I would just use&lt;p&gt;&lt;pre&gt;&lt;code&gt;  for idx, (elt1, elt2) in enumerate(zip(list_1, list_2))
&lt;/code&gt;&lt;/pre&gt;
The following is not necessarily good advice, and he should take his own earlier advice and validate his claims with some profiling.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  &amp;#62; [map is ] much faster, since the loop takes place entirely in
  &amp;#62; the C API and never has to bind loop variables to Python
  &amp;#62; objects.

  &amp;#62; If you find yourself making the same list comprehension
  &amp;#62; repeatedly, make utility functions and use map and/or filter...  
&lt;/code&gt;&lt;/pre&gt;
Note that the following &quot;map&quot; snippet is considerably slower than the corresponding list-comprehension snippet.  Function calls are expensive in python.  Also, his claim that you save time with a &quot;map&quot; by avoiding the loop variable is obviously bogus: you still have to bind the variables in the signature of the  function you&apos;re mapping, unless the signature is empty.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  met% python -m timeit &quot;[x**3 for x in xrange(10000)]&quot;
  1000 loops, best of 3: 1.27 msec per loop
  met% python -m timeit &quot;map(lambda x: x**3, xrange(10000))&quot;
  1000 loops, best of 3: 1.88 msec per loop&lt;/code&gt;&lt;/pre&gt;</text></comment>
<story><title>Python idioms</title><url>http://jaynes.colorado.edu/PythonIdioms.html</url><text></text></story><parent_chain></parent_chain><comment><author>Ysx</author><text>Good stuff, though there&apos;s a few outdated idioms:&lt;p&gt;Lists, or any iterable, can be reverse-sorted with reversed(sorted(my_list)). That&apos;ll give you an iterator, though you can call list() on the result if you need it.&lt;p&gt;&quot;while True&quot; should be used in-place of &quot;while 1&quot;. Reads better.&lt;p&gt;In Python 2, xrange() is preferred over range() when looping - it won&apos;t create an in-memory list of integers, and behaves mostly the same as range but for a few edge cases. Python 3 renamed xrange() to range(), and removed the original range() function.</text></comment>