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 8248311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:08:57+00:00 2026-06-07T23:08:57+00:00

Up to this point in time, in Python, I’ve only ever seen list comprehensions

  • 0

Up to this point in time, in Python, I’ve only ever seen list comprehensions that specify the inclusion of one element at time. For example, they’re all in the following form

[f(x) for x in <iterable>]

Is there any way to specify more than one element at a time? For example,

[f(x), g(x) for x in <iterable>]

I’m asking because I want to create a list comprehension that calculates all the divisors of some number x, but I want to do this as efficiently as possible. Right now I’m using the following,

[y for y in range(1,x+1) if x%y == 0]

but I’d like to do something like this,

[y, x/y for y in range(1, sqrt(x)+1) if x%y == 0]

as this would be more efficient. Btw, I have no practical reason for doing this. It’s simply a challenge problem that somebody told me and the goal is to do it with the smallest, most efficient list comprehension possible.

Thanks in advance.

Edit: Ok, it looks like I have to use tuples. I was trying to avoid that though as I’d then have to make another function to flatten the list which would make my solution longer.

Edit 2: Just in case anyone stumbles upon this question, the answer to what I wanted to do in my original question is this:

[y for x in <iterable> for y in f(x), g(x)]

It uses nested for loops in the list comprehension to get the job done.

  • 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-07T23:09:01+00:00Added an answer on June 7, 2026 at 11:09 pm

    You can assign to tuples

    [(y, x/y) for y in range(1, int(sqrt(x))+1) if x%y == 0]
    

    Not really related to your basic question, but your example: I had to convert the 2nd parameter of range() to an int since sqrt() resulted in a float in my test code.

    Update re Edit in post:

    To flatten this list of tuples:

    In [24]: s
    Out[24]: [(1, 20), (2, 10), (4, 5)]
    

    use this:

    In [25]: import operator
    

    create a tuple:

    In [26]: reduce(operator.add, s, ())
    Out[26]: (1, 20, 2, 10, 4, 5)
    

    create a list:

    In [27]: list(reduce(operator.add, s, ()))
    Out[27]: [1, 20, 2, 10, 4, 5]
    

    Note: In a helpful comment @jamylak points out that reduce and operator.add run order O(N^2), and that using itertools.chain would be much more efficient. This becomes more important as the size of the list grows and should be considered in that case.

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

Sidebar

Related Questions

The problem I am having at this point in time (being new to Python)
I'm convinced at this point that I should be creating subclasses of std::exception for
I'm trying (yet again) to get better at programming, this time in python, and
I have some python scripts that configure linux computers. One of the tasks is
I lost a little bit of time in this Python for statement: class MyListContainer:
I have a boost::posix_time::ptime that points to March 31st 2010 like this: ptime p(date(2010,
At this point I'm not sure if these leaks might be CoreData related or
Up to this point, I have used the following code to restrict the application
Up to this point we have not yet needed a new masterpage for our
I am a bit confused at this point on what is an object, what

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.