Newbie looking for feedback

DaffyDuckMW daffyduckmw at my-deja.com
Fri Sep 15 10:26:37 EDT 2000


> in=open('C:\\foo.txt','r').readlines()
> #Opens the input file and reads each line into a list entry
> out=open('C:\\bar.txt','w')
> #Open output file
> for x in in[:]:
> #Loop once for every line
> 	if len(x)>1:
> 	#Test to see if the line has anything....
> 		out.write(x)
> 		#It does! Write it!

Ok, this is a little off the original subject but I'm curious so I'll
ask it anyway. In the for line, the syntax "for x in lines[:]:" is
used. I understand that the "[:]" provides a slice that includes the
whole list and that this syntax can be used to basically obtain a copy
of the list (right?). But in similar situations so far I've just used
the syntax "for x in lines:". What is the advantage to adding the
"[:]"? Does this affect performance, or accuracy? Also, does this
affect what lines get processed if you mess with the list inside the
loop?



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list