Suppose I have a list that I wish not to return but to yield values from. What is the most pythonic way to do that?
Here is what I mean. Thanks to some non-lazy computation I have computed the list ['a', 'b', 'c', 'd']
, but my code through the project uses lazy computation, so I'd like to yield values from my function instead of returning the whole list.
I currently wrote it as following:
my_list = ['a', 'b', 'c', 'd']
for item in my_list:
yield item
But this doesn't feel pythonic to me.
Use iter
to create a list iterator e.g.
return iter(List)
though if you already have a list, you can just return that, which will be more efficient.