File to dict
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Fri Dec 7 08:00:23 EST 2007
On Fri, 07 Dec 2007 04:44:25 -0800, mrkafk wrote:
> Duncan Booth wrote:
>> But why do you construct a dict from that input data simply to throw it
>> away?
>
> Because comparing strings for equality in a loop is writing C in
> Python, and that's exactly what I'm trying to unlearn.
>
> The proper way to do it is to produce a dictionary and look up a value
> using a key.
>
>>If you only want 1 domain from the file just pick it out of the list.
>
> for item in list:
> if item == 'searched.domain':
> return item...
>
> Yuck.
I guess Duncan's point wasn't the construction of the dictionary but the
throw it away part. If you don't keep it, the loop above is even more
efficient than building a dictionary with *all* lines of the file, just to
pick one value afterwards.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list