I am trying to fetch the size of list in bytes and also size of string in bytes.
If we see the output below for the code, size of list is shown as 52 bytes, where as when I join the list and check the size, output is 35 bytes. At last I tried to get size of string "Iamtestingsize" the output was again 35 bytes. So, size of string after "join" and also size of string "Iamtestingsize" is same.
I have 2 questions here:
1) why is the size of list showing a different output ? also, please let me know if you have any idea on how to fetch the size of contents of list ?
2) I thought, 1 byte == 1 character and I was expecting size of strings msgstr and string will show as 14 bytes instead of 35. Please let me know if am missing anything here ?
3) when I perform len() on list and strings, for msgstr and string - 14 was returned whereas length of list returned 4 which is as I expected.
import sys
list = ['I', 'am', 'testing', 'size']
print sys.getsizeof(list)
msgstr = "".join(list)
print "size of msgstr is " + str(sys.getsizeof(msgstr))
print msgstr
string = "Iamtestingsize"
print "size of str is " + str(sys.getsizeof(string))
print len(list)
print len(msgstr)
print len(string)
Output:
52
size of msgstr is 35
Iamtestingsize
size of str is 35
4
14
14
Note: I am using python 2.7
size-len==37consistently. A list and a string require different amounts of memory because they are completely different types of object. There's no reason they should be the same.