Finding size of Variable

Ayushi Dalmia ayushidalmia2604 at gmail.com
Wed Feb 5 06:35:05 CET 2014


On Wednesday, February 5, 2014 12:59:46 AM UTC+5:30, Tim Chase wrote:
> On 2014-02-04 14:21, Dave Angel wrote:
> 
> > To get the "total" size of a list of strings,  try (untested):
> 
> > 
> 
> > a = sys.getsizeof (mylist )
> 
> > for item in mylist:
> 
> >     a += sys.getsizeof (item)
> 
> 
> 
> I always find this sort of accumulation weird (well, at least in
> 
> Python; it's the *only* way in many other languages) and would write
> 
> it as
> 
> 
> 
>   a = getsizeof(mylist) + sum(getsizeof(item) for item in mylist)
> 
> 
> 
> -tkc

This also doesn't gives the true size. I did the following:

import sys
data=[]
f=open('stopWords.txt','r')

for line in f:
    line=line.split()
    data.extend(line)

print sys.getsizeof(data)

where stopWords.txt is a file of size 4KB



More information about the Python-list mailing list