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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:33:17+00:00 2026-05-17T18:33:17+00:00

Sorry, I just found the id = [conn.cursor() for x in range(100) ] also

  • 0

Sorry, I just found the

id = [conn.cursor() for x in range(100) ]

also works, so my concern will not be a problem anymore.
Thanks for all of your answer, all of you are really fast.


All,

id = [(conn.cursor(),x) for x in range(100) ]
>>> id
[(<sqlite3.Cursor object at 0x01D14DA0>, 0), (<sqlite3.Cursor object at 0x01D14DD0>, 1), (<sqlite3.Cursor object at 0x01D14E00>, 2), (<sqlite3.Cursor object at 0x01D14E30>, 3), (<sqlite3.Cursor object at 0x01D14EC0>, 4), (<sqlite3.Cursor object at 0x01D14EF0>, 5),     <omitted>

but I do not need the id[1] col actually, and I do not want use the

for x in range(100):
    id.append(conn.cursor())

for some reason, do you think I can use the list comprehension to get what I want?
Also similiar question, if I want to invoke one function 100 times.

def foo():
    pass

for x in range(100):
    foo()

Can that “for” be rewrite to a list comprehensions style either?

Thanks!

  • 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-17T18:33:18+00:00Added an answer on May 17, 2026 at 6:33 pm

    For the second question

    List comprehensions are used for generating another list as output of iteration over other list or lists. Since you want to run foo a numer of times, it is more elegant and less confusing to use for .. in range(..) loop.

    If you are interested in collating the return value of foo, then you should use list comprehension else for loop is good. At least I would write it that way.

    See the example below:

    >>> [x for x in range(10)]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> def foo(): print 'foo'
    ... 
    >>> 
    >>> [foo() for x in range(10)]
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    [None, None, None, None, None, None, None, None, None, None]
    >>> 
    

    [Edit: As per request]

    The iter version that was provided by eumiro.

    >>> results = ( foo() for _ in xrange(10) )
    >>> results
    <generator object <genexpr> at 0x10041f960>
    >>> list(results)
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    foo
    [None, None, None, None, None, None, None, None, None, None]
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry just found this code here - http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html and the mutex was explained with
Sorry folks...first post here. Just getting the hang of this site. Thanks for the
Sorry if this is a comp-sci 101 question. I'm just unsure if I'm missing
Sorry, if this is a noobish question, but I'm just getting started with Rails
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry for the second newbie question, I'm a developer not a sysadmin so this
I'm not a front-end programmer, but I first realized the issue just when I
Sorry guys if this is a lame question, but the examples i found dont
I just moved my Wordpress site from using nginx to Apache2, and found that
I just downloaded the mysql-proxy and created this script lua (found in Mysql docs):

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.