Skip to content Skip to sidebar Skip to footer

Different Size Of Element In Numpy Array And List

I am using Python 3.4 32 bits on win 7. I found that an integer in an numpy array has 4 bytes, but in a list it has 10 bytes. import numpy as np s = 10; lt = [None] * s; cnt = 0

Solution 1:

The element size in arrays is easy - it's determined by the dtype, and as your code shows can be found with .itemsize. 4bytes is common, such as for np.int32, np.float64. Unicode strings are also allocated 4 bytes per character - though the real unicode uses a variable number of characters.

The per element size for lists (and tuples) is trickier. A list does not contain the elements directly, rather it contains pointers to objects which are stored elsewhere. Your list size records the number of pointers, plus a pad. The pad lets it grow in size (with .append) efficiently. All your lists have the same size, regardless of 'first item' size.

My data:

In [2324]: lt=[None]*10In [2325]: sys.getsizeof(lt)
Out[2325]: 72In [2326]: lt=[i for i inrange(10)]
In [2327]: sys.getsizeof(lt)
Out[2327]: 96In [2328]: lt=['A'for i inrange(10)]
In [2329]: sys.getsizeof(lt)
Out[2329]: 96In [2330]: lt=['AB'for i inrange(10)]
In [2331]: sys.getsizeof(lt)
Out[2331]: 96In [2332]: lt=['ABCDEF'for i inrange(10)]
In [2333]: sys.getsizeof(lt)
Out[2333]: 96In [2334]: lt=[Nonefor i inrange(10)]
In [2335]: sys.getsizeof(lt)
Out[2335]: 96

and for the corresponding arrays:

In [2344]: lt=[None]*10; a=np.array(lt)
In [2345]: a
Out[2345]: array([None, None, None, None, None, None, None, None, None, None], dtype=object)
In [2346]: a.itemsize
Out[2346]: 4In [2347]: lt=['AB'for i inrange(10)]; a=np.array(lt)
In [2348]: a
Out[2348]: 
array(['AB', 'AB', 'AB', 'AB', 'AB', 'AB', 'AB', 'AB', 'AB', 'AB'], 
      dtype='<U2')
In [2349]: a.itemsize
Out[2349]: 8

When the list contains None, the array is object dtype, and the elements are all pointers (4 bytes integers).

Post a Comment for "Different Size Of Element In Numpy Array And List"