Re: [Tutor] construct
Thanks all On Friday 04 November 2005 11:53 am, Kent Johnson wrote: > Eric Walker wrote: > > All, > > Whats the construct that allows to loop through a list and perform an > > action? you also can add a test at the end. something like myfile = > > [print name for name in yourlist, if ...] > > It's called a list comprehension. You can see some examples here: > http://docs.python.org/tut/node7.html#SECTION00714 > > It won't work with your example though - 'print' is a statement, not an > expression, and cannot be used in a LC. You could use sys.stdout.write(name > + '\n') instead if you like. > > Personally I think it is bad style to use a LC for its side effects; in > those cases I prefer to write the loop out. > > Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] construct
Eric Walker wrote: > All, > Whats the construct that allows to loop through a list and perform an action? > you also can add a test at the end. something like myfile = [print name for > name in yourlist, if ...] It's called a list comprehension. You can see some examples here: http://docs.python.org/tut/node7.html#SECTION00714 It won't work with your example though - 'print' is a statement, not an expression, and cannot be used in a LC. You could use sys.stdout.write(name + '\n') instead if you like. Personally I think it is bad style to use a LC for its side effects; in those cases I prefer to write the loop out. Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] construct
On Fri, 4 Nov 2005, Eric Walker wrote: > Whats the construct that allows to loop through a list and perform an > action? you also can add a test at the end. something like myfile = > [print name for name in yourlist, if ...] I need to look up how to use > the test command and I forgot the name of the construct. Hi Eric, I think you're looking for list comprehensions. Here you go: http://www.python.org/peps/pep-0202.html If you have more questions, please feel free to ask! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] construct
All, Whats the construct that allows to loop through a list and perform an action? you also can add a test at the end. something like myfile = [print name for name in yourlist, if ...] I need to look up how to use the test command and I forgot the name of the construct. Thanks.. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor