Pop a list from beginning ? and memory saving...

Peter Hansen peter at engcorp.com
Sun Jun 2 07:50:14 EDT 2002


Shagshag13 wrote:
> 
> I had a huge list containing consuming data, as i only need a one pass in a
> for statement, i'm wondering if by doing this with a pop i could save memory ? 

Maybe, maybe not.  The implementation _probably_ makes a copy of
the entire list at some point as you shrink it.  That would mean that
at least at some interval, you would temporarily consume almost twice
the memory of the original list as the original list is copied, then 
freed.  You might end up with less memory used, but with double the 
memory needed.  Of course, the same would have held true as you built
the list in the first place...

Why are you trying to save memory?  Are you really working with such
a large amount of data in one chunk that you will run out?  Maybe
there is some other approach you haven't thought of.  This effort
almost smells like premature optimization (the root of all evil).

-Peter



More information about the Python-list mailing list