Lists, tuples and memory.

Terry Reedy tjreedy at udel.edu
Fri Jul 16 00:01:40 EDT 2004


"Larry Bates" <lbates at swamisoft.com> wrote in message
news:CLednf1kspFPeGvd4p2dnA at comcast.com...
> Any reason not to put the words into a dictionary?

for spell checking, I think I would...

> Then your code becomes:
>
> lstdict=dict([(x.lower().strip(), None) for x in
>               file("D:\\CommonDictionary.txt")])

The list comp creates an unnecessary (and, in this case, large)
intermediate list.
In 2.4, removing the [] pair would result in a generator expression.
In the meanwhile...

>>> def lostrip(src):
...   for w in src: yield w.lower().strip(), None
...
>>> t = dict(lostrip(['abc ', 'DEG\n']))
>>> t
{'abc': None, 'deg': None}

Terry J. Reedy






More information about the Python-list mailing list