Better HN
topnewbestaskshowjobs
Log inSign up

aromaticrose

5 karma · joined Jul 4, 2023 (3y ago) · 3 submissions

Submissions

comment 2y ago · link · parent

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.

comment 2y ago · link · parent

Maybe it's just too early in the morning for me or I don't know enough about Python, but why would the OPs code snippet keep the list in memory, and why is yours an improvement?

Also, is this something you could handwave because of Python's garbage collection, or is that not going to help in this case?

comment 3y ago · link · parent

GrapheneOS is under active development and has interesting privacy and security features.

https://grapheneos.org/

https://grapheneos.org/features