470,636 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,636 developers. It's quick & easy.

Is there any Iterator type example?


Is there any example about how to use Iterator type?

Nov 22 '05 #1
1 1634
How about this one:

import os
from os.path import join

def py_files(dir):
for root, dirs, files in os.walk(dir):
for name in files:
if name.lower().endswith(".py"):
yield join(root,name )

if __name__=="__main__":
dir= "C:\\Python23\\"
for f in py_files(dir):
print f

In my opinion, iterators have two advantages:
1. They can make it easier to iterate over complicated structures, like
a file tree in the above example.
2. Since they store only information on how to access the next item,
they need often less memory. For example, map(f, list) creates a
list [f(L) for L in list], while itertools.imap(f,list) creates an
iterator object.
The iterator object often needs much less memory than the new list.

There was also a dicussion on this topic on the tutor list two days
ago, maybe you'd like to join.

Kind regards,

Thomas Moore schrieb:

Is there any example about how to use Iterator type?


Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by Grant Edwards | last post: by
26 posts views Thread by Michael Klatt | last post: by
2 posts views Thread by wenmang | last post: by
7 posts views Thread by Prawit Chaivong | last post: by
18 posts views Thread by silversurfer | last post: by
reply views Thread by mailforpr | last post: by
1 post views Thread by atomik.fungus | last post: by
27 posts views Thread by Steven D'Aprano | last post: by
3 posts views Thread by vasili | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.