How to Pythonically yield all values from a list?

Posted by bodacydo on Stack Overflow See other posts from Stack Overflow or by bodacydo
Published on 2010-03-23T08:17:57Z Indexed on 2010/03/23 8:23 UTC
Read the original article Hit count: 505

Filed under:
|
|

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:

List = ['a', 'b', 'c', 'd']
for item in List:
    yield item

But this doesn't feel Pythonic to me.

Looking forward to some suggestions, thanks.

Boda Cydo.

© Stack Overflow or respective owner

Related posts about python

Related posts about yield