Lists, tuples and memory.

Christopher T King squirrel at WPI.EDU
Thu Jul 15 15:52:53 EDT 2004


On 15 Jul 2004, Elbert Lev wrote:

> But then I decieded to compare this pieces:
> 
> t = tuple(file("D:\\CommonDictionary.txt"))        # 1
> lstdict = map(lambda x: x.lower().strip(), t)      # 2
> 
> lstdict = map(lambda x: x.lower().strip(),
> file("D:\\CommonDictionary.txt")) # 3
> 
> As expected, after line 2 memory was 5550K, but after line 3 it jumped
> to 7996K!!!
> 
> The question:
> 
> If refference counting is used, why the second assignment to lstdict
> (line 3) did not free the memory alocated by the first one (line 2)
> and reuse it?

Because after line 2, you still have a 2MB tuple sitting around in 't'. ;)

Try this:

t = tuple(file("D:\\CommonDictionary.txt"))        # 1
lstdict = map(lambda x: x.lower().strip(), t)
del t                                              # 2

lstdict = map(lambda x: x.lower().strip(),
file("D:\\CommonDictionary.txt")) # 3

Python doesn't know that you're not going to try to access t at some 
future point, even though there are no more accesses in your code.

A couple of other thoughts on what you're doing:

1) Stick with lists.  Tuples are meant to be used not really as immutable
lists, but as a way to group related items of different types.  e.g. the
sequence 'apple','orange','pear','banana' should be stored in a list,
whereas the group of related items 'apple','red',3,5 (presumably
describing an apple in some predefined manner, say,
fruit,color,width,height) should be stored in a tuple.  At least that's 
what Guido wants us to do. ;)

2) Assuming you're using a newer version of Python, try using a list 
comprehension instead of map().  It's a little bit cleaner, and will 
probably be a bit faster too:

lstdict = [x.lower().strip() for x in file("D:\\CommonDictionary.txt")]

If you've never worked with them before, list comprehensions are a new 
syntax intended to replace map(), filter(), and other such constructs with 
one unified syntax.  They're usually very easy to read and write (as the 
one above is) since they help you avoid using lambda.




More information about the Python-list mailing list