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

  • Home
  • SEARCH
  • 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 6789017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:33:14+00:00 2026-05-26T17:33:14+00:00

Many developers I have met suggest it’s best practice to go with simple loops

  • 0

Many developers I have met suggest it’s best practice to go with simple loops and if conditions instead of one line list comprehension statements.

I have always found them very powerful as I can fit a lot of code in a single line and it saves a lot of variables from being created. Why is it still considered a bad practice?

(Is it slow?)

  • 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-05-26T17:33:15+00:00Added an answer on May 26, 2026 at 5:33 pm

    List comprehensions are used for creating lists, for example:

    squares = [item ** 2 for item in some_list]
    

    For loops are better for doing something with the elements of a list (or other objects):

    for item in some_list:
        print(item)
    

    Using a comprehension for its side effects, or a for-loop for creating a list, is generally frowned upon.


    Some of the other answers here advocate turning a comprehension into a loop once it becomes too long. I don’t think that’s good style: the append calls required for creating a list are still ugly. Instead, refactor into a function:

    def polynomial(x):
        return x ** 4 + 7 * x ** 3 - 2 * x ** 2 + 3 * x - 4
    result = [polynomial(x) for x in some_list]
    

    Only if you’re concerned about speed – and you’ve done your profiling! – you should keep the long, unreadable list comprehension.

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

Sidebar

Related Questions

Is there a connection limit on Sql Server 2005 Developers Edition. We have many
I have seen many developers that add various convenience macros to the Prefix.pch of
I'm using a powerful library called TiMidity, which I'm sure many iPhone developers have
I have seen many developers writing HTML or CSS inline style widths of 99.9%
We have a project with many developers. Some use Visual Studio 2008 Professional with
Many iOS developers have found the Cocoa Lumberjack Logging framework to fill a need
I have heard many developers refer to code as legacy. Most of the time
I have a many-to-many (developers & projects) relationship modeled using SA's association_proxy . The
We have a SVN set up across many developers. i am trying to import
Much to annoyance of many developers Microsoft have removed the Run and Run On

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.