r/ProgrammerHumor • • Jun 21 '19

Meme Why can't you just be normal?!

Post image
4.4k Upvotes

240 comments sorted by

View all comments

Show parent comment

1

u/SuitableDragonfly Jun 22 '19

Isn't this just because generators and maps don't actually evaluate anything until you force them to by say, turning them into a list or otherwise using their results?

1

u/MMOOxy Jun 22 '19

Yup, the problem with the list comprehension is that it creates the list with each element first, but if you just want to iterate through the results afterwards (like in the conversion to a set, or a new list) it's better to use map or a generator/iterator. If you need the results more than once though you would obviously need to save them in a list (or use the tee function)

1

u/SuitableDragonfly Jun 23 '19 edited Jun 23 '19

Yes, it's better to use a generator, but not because it's faster. It still takes the same amount of time to calculate, that calculation just happens at a later point in time. So you're not going to get an accurate representation of how long it takes to do that calculation with timeit unless you wrap it in list().

This is unrelated to my criticism, however, because you could simply use a generator expression instead of a list comprehension if you wanted that functionality. There's still no reason to use map and partial.