Splitting a list of strings
Alex Martelli
aleax at aleax.it
Wed Sep 18 11:41:08 EDT 2002
Emile van Sebille wrote:
...
> for line in lines:
> vals["@#".find(line[:1])+1].append(line)
...
> Or, if you want to use an lc:
>
> [ vals["@#".find(line[:1])+1].append(line) for line in lines]
The lc variant wantonly produces and throws away a list of
copies of None, one per line in lines. It's thus likely to
be quite a bit slower than the simpler loop.
IMHO, vals should be a dictionary indexed by the character
used to discriminate -- using vals as a list and doing a
find operation repeatedly seems a waste. However, I admit
I haven't timed the two alternatives, so I could be wrong!
Alex
More information about the Python-list
mailing list