# [Tutor] a quick Q: how to use for loop to read a series of files with .doc end

Andreas Perstinger andreas.perstinger at gmx.net
Sat Oct 8 07:57:37 CEST 2011

```On 2011-10-08 05:34, lina wrote:
> Another minor derived questions:
>
>          summary=[]
>          for a,b in zip(results['E'],results['B']):
>              summary.append(a+b)    ## now the summary is '[0,1, 3, 5,
> 6,0,0,0]'
>          del summary[0]   ## here I wanna remove the first zero, which came
> from the initial double quote "EB...E",
>          summary.pop()    ## here I wanna remove the last three zeros
>          summary.pop()
>          summary.pop()
>          print(summary)   ### output is [1,3,5,6]
>          summary='\n'.join(str(summary).split(','))  ### wish the result in
> one column,
>          with open(base+OUTFILEEXT,"w") as f:
>              f.write(str(summary))
>
> the final result in the output.txt file is:
> [1
> 3
> 5
> 6]
>
> Q1: how can I remove the [1 "[" to only keep 1?

That's a job for a list comprehension (read
http://docs.python.org/py3k/tutorial/datastructures.html#list-comprehensions
for a short introduction):

new_summary = '\n'.join([str(element) for element in summary])

> Q2 how can I improve above expressions?

-) As above, you could use a list comprehension for creating your
summary list:

summary = [(a + b) for a, b in zip(results['E'], results['B'])]

del summary[0]
summary.pop()
summary.pop()
summary.pop()

I suggest to slice your list:

summary = summary[1:-3]

But this assumes that your summary list will always have one leading
zero and 3 trailing zeros.