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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:46:12+00:00 2026-06-15T07:46:12+00:00

In many case, people all ways say use the yield to lazily create element.

  • 0

In many case, people all ways say “use the yield to lazily create element.”
but I think everything have cost, include the yield and its iterator.

In effective nord eyes, I think it’s nice question.
so,for example, when I get an function.

def list_gen(n):
    if n > MAGIC_NUM:
        return xrange(n)
    else:
        return range(n)

How much dose the MAGIC_NUM is?

UPDATE sorry for this mistake, I’m origin meaning is compare the iterator’s cost and list cost.

UPDATE AGAIN Please imaging an case. Whether have an condition, that the memory so limit that it’s can’t create an iterator.

ha, this question is more funny now.
UPDATE AGAIN Why does create an iterator and save the yield context are less then create a list? or How much does iterator cost ?(sorry for my insult) How many bytes?

  • 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-15T07:46:13+00:00Added an answer on June 15, 2026 at 7:46 am

    You’re mixing several things up.

    def list_gen(n):
        i=0
        while i<n:
            yield i
            i += 1
    

    This function is a generator. Calling it returns a generator object, which is an iterator.

    An iterator is a thing that has next(), i.e. it can be traversed over once. An iterator is created over something using iter whenever you do a for i in something.

    def list_gen(n):
        return range(n)
    
    def list_gen(n):
        return xrange(n)
    

    These functions are regular functions. One returns a list and the other returns an xrange object. Both lists and xranges are iterable, i.e. multiple independent iterators can be created for them.


    So back to your question: You’re asking whether to return a list or an xrange object.

    That depends, obviously! It depends on what you want to do with the result.

    • If you want to mutate it somehow, then you need a real list. Use range directly.

    • If you only want to iterate over it, then it doesn’t make a difference semantically: both an xrange object and a list returned by range will produce an iterator which iterates over the same sequence.

      However, if you use xrange, you’ll never create the whole list in memory. Why create a full-fledged list object in memory if all you want to do is a simple iteration? You don’t need to allocate a temporary large memory buffer whenever you want a for loop, right?

    Hence: It’s safe to stick with xrange, since the caller can always make a list out of it.


    Let’s confirm that with a benchmark. We want to know if it’s faster to iterate over xranges than over lists constructed by range (including the cost of range call, of course).

    Code:

    import timeit
    
    ns = [1,2,3, 5, 10, 50, 100]
    print 'n', '\t', 'range', '\t', 'xrange'
    for n in ns:
        t1 = timeit.timeit("for i in range({}): pass".format(n))
        t2 = timeit.timeit("for i in xrange({}): pass".format(n))
        print n, '\t', t1, '\t', t2
    

    Result:

    n       range           xrange
    1       0.566222990493  0.418698436395
    2       0.594136874362  0.477882061758
    3       0.630704800817  0.488603362929
    5       0.725149288913  0.540597548519
    10      0.90297752809   0.687031507818
    50      2.44493085566   1.89102105759
    100     4.31189321914   3.33713522433
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many ways to Title Case in java . But how do you
I've got a fairly important conceptual issue that many people have asked about, but
Let's say I have a case class that represents personas, people on different social
Many people have the same problem, the R file. In my case, i have
Many people have faced similar issues, and I think I have followed and fixed
I have two distinct scenarios. One, where there is a many to many case,
I have a collection testcase like this, Test Builds have many Test Case Logs
I have not used many lambda expressions before and I ran into a case
I have a case where i have three entities with one-to-many and one-to-many relationships:
I have a table: Order Detail. An Order has many details, in my case

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.