i can't think of anything but a loop here UNLESS you take the list from its source one element at a time, process it & then print the result.
example of this would be : list comes in from standard input. list comes from a database list is read from a file. so again where the list comes from is important. if its standard input then you program will be easy & won't use any memory i guess. import sys # cgi & cgitb if going web data = sys.stdin.readline() <do what ever you like with data> print data On Wed, Apr 9, 2008 at 8:15 PM, Dinesh B Vadhia <[EMAIL PROTECTED]> wrote: > Sorry, let's start again. > > Here is a for loop operating on a list of string items: > > data = ["string 1", "string 2", "string 3", "string 4", "string 5", > "string 6", "string 7", "string 8", "string 9", "string 10", "string 11"] > > result = "" > for item in data: > result = <some operation on> item > print result > > I want to replace the for loop with another structure to improve > performance (as the data list will contain >10,000 string items]. At each > iteration of the for loop the result is printed (in fact, the result is sent > from the server to a browser one result line at a time) > > The for loop will be called continuously and this is another reason to > look for a potentially better structure preferably a built-in. > > Hope this makes sense! Thank-you. > > Dinesh > > > > ----- Original Message ----- *From:* Kent Johnson <[EMAIL PROTECTED]> > *To:* Dinesh B Vadhia <[EMAIL PROTECTED]> > *Cc:* tutor@python.org > *Sent:* Wednesday, April 09, 2008 12:40 PM > *Subject:* Re: [Tutor] List comprehensions > > Dinesh B Vadhia wrote: > > Here is a for loop operating on a list of string items: > > > > data = ["string 1", "string 2", "string 3", "string 4", "string 5", > > "string 6", "string 7", "string 8", "string 9", "string 10", "string > 11"] > > > > result = "" > > for item in data: > > result = item + "\n" > > print result > > I'm not sure what your goal is here. Do you mean to be accumulating all > the values in data into result? Your sample code does not do that. > > > I want to replace the for loop with a List Comrehension (or whatever) to > > > improve performance (as the data list will be >10,000]. At each stage > > of the for loop I want to print the result ie. > > > > [print (item + "\n") for item in data] > > > > But, this doesn't work as the inclusion of the print causes an invalid > > syntax error. > > You can't include a statement in a list comprehension. Anyway the time > taken to print will swamp any advantage you get from the list comp. > > If you just want to print the items, a simple loop will do it: > > for item in data: > print item + '\n' > > Note this will double-space the output since print already adds a newline. > > If you want to create a string with all the items with following > newlines, the classic way to do this is to build a list and then join > it. To do it with the print included, try > > result = [] > for item in data: > newItem = item + '\n' > print newItem > result.append(newItem) > result = ''.join(result) > > Kent > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > >
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor