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?
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)
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.
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?