Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:42:09+00:00 2026-06-18T02:42:09+00:00

In Python, I really enjoy how concise an implementation can be when using list

  • 0

In Python, I really enjoy how concise an implementation can be when using list comprehension. I love to do concise list comprehensions this:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = [x for x in myList if x > 10]

However, I often encounter more verbose implementations like this:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = []
for i in xrange(0, len(myList)):
    if myList[i] > 10:
        bigNumbers.append(myList[i])

When a for loop only looks through one data structure (e.g. myList[]), there is usually a straightforward list comprehension statement that is equivalent to the loop.
With this in mind, is there a refactoring tool that converts verbose Python loops into concise list comprehension statements?


Previous StackOverflow questions have asked for advice on transforming loops into list comprehension. But, I have yet to find a question about automatically converting loops into list comprehension expressions.


Motivation: 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:42:10+00:00Added an answer on June 18, 2026 at 2:42 am

    2to3 is a refactoring tool that can perform arbitrary refactorings, as long as you can specify them with a syntactical pattern. The pattern you might want to look for is this

    VARIABLE1 = []
    for VARIABLE2 in EXPRESSION1:
        if EXPRESSION2:
            VARIABLE1.append(EXPRESSION3)
    

    This can be refactored safely to

    VARIABLE1 = [EXPRESSION3 for VARIABLE2 in EXPRESSION1 if EXPRESSION2]
    

    In your specific example, this would give

    bigNumbers = [myList[i] for i in xrange(0, len(myList)) if myList[i] > 10]
    

    Then, you can have another refactoring that replaces xrange(0, N) with xrange(N),
    and another one that replaces

    [VARIABLE1[VARIABLE2] for VARIABLE2 in xrange(len(VARIABLE1)) if EXPRESSION1]
    

    with

    [VARIABLE3 for VARIABLE3 in VARIABLE1 if EXPRESSION1PRIME]
    

    There are several problems with this refactoring:

    • EXPRESSION1PRIME must be EXPRESSION1 with all occurrences of
      VARIABLE1[VARIABLE2] replaced by VARIABLE3. This is possible with
      2to3, but requires explicit code to do the traversal and replacement.
    • EXPRESSION1PRIME then must not contain no further occurrences of
      VARIABLE1. This can also be checked with explicit code.
    • One needs to come up with a name for VARIABLE3. You have chosen x;
      there is no reasonable way to have this done automatically. You could
      chose to recycle VARIABLE1 (i.e. i) for that, but that may be confusing
      as it suggests that i is still an index. It might work to pick a synthetic
      name, such as VARIABLE1_VARIABLE2 (i.e. myList_i), and check whether
      that’s not used otherwise.
    • One needs to be sure that VARIABLE1[VARIABLE2] yields the same as you get
      when using iter(VARIABLE1). It’s not possible to do this automatically.

    If you want to learn how to write 2to3 fixers, take a look at Lennart Regebro’s book.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At this time Textmate's official Python bundle is really bare bones, especially in comparison
I've been using arrays lately and really missing Python's in operator. e.g.: if (hello
I really thought I understood Python variable referencing, so I'm confused why this code
I really love working with Django and Python, when I go back to PHP
Thanks to python-spidermonkey , using JavaScript code from Python is really easy. However, instead
This is really a python language question, but its wrapped around a Google appengine
I'm learning python and Django coming from PHP. This is all really exciting, and
I often use R to analyze data, and really enjoy Notepad++ along with NppToR.
I'm new to python and I really like concept of using dictionaries instead of
I'm new to Python and I really love the min function. >>>min([1,3,15]) 0 But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.