0

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

1
  • 1
    There is some constant overhead in the memory taken up by a string object. When I tried it, I got size-len==37 consistently. 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. Commented Oct 17, 2016 at 11:44

1 Answer 1

2
  1. A list (any list) data structure requires additional maintenance overhead to keep elements inside it. This overhead is reflected in the difference of the results of getsizeof.

  2. Python strings are a text sequence type - str, and not C strings or anything alike. Same as Python lists, there is associated metadata involved, beyond the contents of the string alone:


Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getsizeof(b'asd')
40
>>> sys.getsizeof('asd')
40
>>> sys.getsizeof(u'asd')
56

  1. The length of a string is, intuitively, defined as the number of characters in it.
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.