Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are sets bigger than lists in python?

Tags:

python

list

set

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

a = set(range(10000))
b = list(range(10000))
print('set size = ', a.__sizeof__())
print('list size = ', b.__sizeof__())

output:

set size = 524488
list size = 90088
like image 330
banx Avatar asked Aug 28 '10 17:08

banx


1 Answers

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.

like image 155
Dave Webb Avatar answered Nov 12 '22 21:11

Dave Webb