How do I speedup this loop?

Bart Nessux bart_nessux at
Wed Jul 14 21:40:15 CEST 2004

george young wrote:
> How about:
> lines = []
> out = os.popen('some command')
> for l in out:
>     lines.append(l.strip())
> script = ''.join(lines)
> out.close()
> The "strip" actually removes white space from front and back of the string;
> you could say l.strip('\n') if you only want the newlines removed (or '\r'
> if they're really carriage return characters.)

The above is a great solution... should make for a good speed up. It's 
how I might pproach it.

> Or if you want a clever (and most CPU efficient!) one-liner:
> script = [l.strip() for l in os.popen('some command')]

Clever progammers should be shot! I've had to work behind them... they 
are too smart for their own good. They think everyone else in the world 
is as clever as they are... this is where they are wrong ;)

More information about the Python-list mailing list