Find an Item in a Sorted List

HankC hankc at nospam.com
Wed Feb 27 08:45:34 CET 2002


On 26 Feb 2002 19:50:56 -0800, danb_83 at yahoo.com (Dan Bishop) wrote:

>
>If your data file is small enough to fit in memory, you can use
>
>dict = {}
>datafile = open(..., "r")
>for line in datafile.readlines():
>   name = line.split(",")[0]
>   dict[name] = line[:-1] # strip trailing \n
>datafile.close()
>

Thanks all, for the help.  I have one more question regarding resource
management.  My altered version of the above code:

dict = {}
datafile = open(..., "r")
datalines = datafile.readlines()
for line in datalines:
   name = line.split(",")[0]
   dict[name] = line[:-1] # strip trailing \n
###
datafile.close()
del(datalines)

At the ### mark, I looks like have have two versions of the data
resource - one list and one dictionary.  My questions:

- does the del(datalines) call free the memory (not necessarily
immediately) for the list resource?

- does the original version of the function, by combining readlines()
with the for statement, negate the need for freeing the list?  In
other words, will the datafile.close() statement in itself do the
trick?

I appreciate the patience!  Things like garbage collection and for
loops iterating through lists take a little getting used to.




More information about the Python-list mailing list