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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:21:21+00:00 2026-06-05T19:21:21+00:00

I submitted a pull request with this code: my_sum = sum([x for x in

  • 0

I submitted a pull request with this code:

my_sum = sum([x for x in range(10)])

One of the reviewers suggested this instead:

my_sum = sum(x for x in range(10))

(the difference is just that the square braces are missing).

I was surprised that the second form seems to be identical. But when I tried to use it in other contexts where the first one works, it fails:

y = x for x in range(10)
        ^ SyntaxError !!!

Are the two forms identical? Is there any important reason for why the square braces aren’t necessary in the function? Or is this just something that I have to know?

  • 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-05T19:21:23+00:00Added an answer on June 5, 2026 at 7:21 pm

    This is a generator expression. To get it to work in the standalone case, use braces:

    y = (x for x in range(10))
    

    and y becomes a generator. You can iterate over generators, so it works where an iterable is expected, such as the sum function.

    Usage examples and pitfalls:

    >>> y = (x for x in range(10))
    >>> y
    <generator object <genexpr> at 0x0000000001E15A20>
    >>> sum(y)
    45
    

    Be careful when keeping generators around, you can only go through them once. So after the above, if you try to use sum again, this will happen:

    >>> sum(y)
    0
    

    So if you pass a generator where actually a list or a set or something similar is expected, you have to be careful. If the function or class stores the argument and tries to iterate over it multiple times, you will run into problems. For example consider this:

    def foo(numbers):
        s = sum(numbers)
        p = reduce(lambda x,y: x*y, numbers, 1)
        print "The sum is:", s, "and the product:", p
    

    it will fail if you hand it a generator:

    >>> foo(x for x in range(1, 10))
    The sum is: 45 and the product: 1
    

    You can easily get a list from the values a generator produces:

    >>> y = (x for x in range(10))
    >>> list(y)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    You can use this to fix the previous example:

    >>> foo(list(x for x in range(1, 10)))
    The sum is: 45 and the product: 362880
    

    However keep in mind that if you build a list from a generator, you will need to store every value. This might use a lot more memory in situations where you have lots of items.

    Why use a generator in your situation?

    The much lower memory consumption is the reason why sum(generator expression) is better than sum(list): The generator version only has to store a single value, while the list-variant has to store N values. Therefore you should always use a generator where you don’t risk side-effects.

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

Sidebar

Related Questions

I have a little application I build that monitors tickets submitted. It basically just
I submitted an update for one of my iPhone apps last week and just
I'm about to pull my hair out. I've submitted a pull request from my
I recently submitted a windows phone app to the marketplace that uses the geolocation
I have user submitted content that is loaded into c# winform in our office
I recently submitted an app to iTunes that has in-app purchases. At what specific
I just submitted my iPhone application using iTunes Connect, but I don't see anywhere
I submitted a binary file to Apple without any source code. Apart from manually
I've submitted a change to an Open Source project on Github, and received code
Hey guys, I am trying to create a script that will pull the log's

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.