With sys.getsizeof()
you can check the memory usage of an object:
import sys mylist = range(0, 10000) print(sys.getsizeof(mylist)) # 48
Woah… wait… why is this huge list only 48 bytes?
It’s because the range function returns a class thatonly behaves like a list. A range is a lot more memory efficient than using an actual list of numbers.
You can see for yourself by using a list comprehension to create an actual list of numbers from the same range:
import sys myreallist = [x for x in range(0, 10000)] print(sys.getsizeof(myreallist)) # 87632