Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically simplifying/refactoring Python code (e.g. for loops -> list comprehension)?
    text
    copied!<p>In Python, I really enjoy how concise an implementation can be when using list comprehension. I love to do concise list comprehensions this:</p> <pre><code>myList = [1, 5, 11, 20, 30, 35] #input data bigNumbers = [x for x in myList if x &gt; 10] </code></pre> <p>However, I often encounter more verbose implementations like this:</p> <pre><code>myList = [1, 5, 11, 20, 30, 35] #input data bigNumbers = [] for i in xrange(0, len(myList)): if myList[i] &gt; 10: bigNumbers.append(myList[i]) </code></pre> <p>When a <code>for loop</code> only looks through one data structure (e.g. <code>myList[]</code>), there is usually a straightforward list comprehension statement that is equivalent to the loop. <br> <strong>With this in mind, is there a refactoring tool that converts verbose Python loops into concise list comprehension statements?</strong></p> <hr> <p><a href="https://stackoverflow.com/questions/2532525/how-do-i-make-this-simple-list-comprehension?rq=1">Previous</a> <a href="https://stackoverflow.com/questions/10637037/replacing-while-loop-with-list-comprehension">StackOverflow</a> <a href="https://stackoverflow.com/questions/2532525/how-do-i-make-this-simple-list-comprehension?rq=1">questions</a> have asked for advice on transforming loops into list comprehension. But, I have yet to find a question about <strong>automatically</strong> converting loops into list comprehension expressions.</p> <hr> <p><strong>Motivation:</strong> There are numerous ways to answer the question "what does it mean for code to be clean?" Personally, I find that making code concise and getting rid of some of the fluff tends to make code cleaner and more readable. Naturally there's a line in the sand between "concise code" and "incomprehensible one-liners." Still, I often find it satisfying to write and work with concise code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload