Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The OP's version is creating the list, then processing it.

The second version is an generator; it skips the 'creating the list' part.



Ohhhhh. The coffee's kicked in. Thanks for explaining. To recap, a list comprehension:

1. Creates a new, duplicate list containing every record in housing_records

2. Loops through the new list

3. Applies f() to each element and updates the new list as it loops

4. Sum() sums all the elements in the list by accessing each element and adding it to a total

Note: All returned values of f() are stored in memory at the end of step 3. This is a waste.

A generator:

1. Creates a generator object that contains nothing but acts like a list

2. When sum() accesses an element in the generator object, the generator object applies f() to the element in housing_list and returns the value

3. Sum() is therefore able to sum all the elements by "accessing" each element and adding it to a total

Note: Only one result of f() is stored in memory at any given time. Much better.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: