2

Why this happens? shouldn't the list (with overhead) be bigger than string?

import sys
print(sys.getsizeof('a'*1000)) # output is 1049
print(sys.getsizeof(['a'*1000])) # output is 72

2 Answers 2

1

You are just getting the size of the list object, not what's inside of the list.

You can see the size of what's inside the iterable by looping over the list:

print(sys.getsizeof('a'*1000)) # 1049
print(sys.getsizeof(['a'*1000])) # 72
myl = ['a'*1000]
for x in myl:
    print(sys.getsizeof(x)) # 1049
myl2 = []
print(sys.getsizeof(myl2)) # 64
myl2 = ['a']
print(sys.getsizeof(myl2)) # 72
Sign up to request clarification or add additional context in comments.

Comments

1

The list doesn't store the string, just a reference to the string. Therefore, the size of the list == (overhead of list) + (size of string reference)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.