In the examples below, resp.results is an iterator.
Version1:
items = [] for result in resp.results: item = process(result) items.append(item) return iter(items)
Version 2:
for result in resp.results: yield process(result)
Does it return (elements) in Version 1 better / worse in terms of performance / saving memory than just returning elements?
In the Python Cookbook, Alex says that explicit iter () is “more flexible, but less commonly used,” but what are the pros and cons of returning iter (items) versus output, as in version 2?
Also, what are the best ways to remove an iterator and / or exit? - you can not do len (results) to check the size of the list?
source share