开发者

Why are sets bigger than lists in python?

Why is the size of sets in Python noticeably larger than that of lists with same elements?

a = set(range(10000))
b =开发者_StackOverflow社区 list(range(10000))
print('set size = ', a.__sizeof__())
print('list size = ', b.__sizeof__())

output:

set size = 524488
list size = 90088


The set uses more memory than the list as it stores a table of hashes of all the elements so it can quickly detect duplicate entries and so on. This is why every set member must be hashable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜