Why tuples use less space in memory than lists in Python?

Small tutorial on basic classes in Python with code examples 
17 October 2017   4097

Let's create a Python tuple:

>>> a = (1,2,3)
>>> a.__sizeof__()
48

and list

>>> b = [1,2,3]
>>> b.__sizeof__()
64

Tuple will use less memory than list. Let's figure out why.

 lists are variable-sized while tuples are fixed-size.

So tuples can store the elements directly inside the struct, lists on the other hand need a layer of indirection (it stores a pointer to the elements). This layer of indirection is a pointer, on 64bit systems that's 64bit, hence 8bytes.

But there's another thing that lists do: They over-allocate. Otherwise list.append would be an O(n) operation always - to make it amortized O(1) (much faster) it over-allocates. But now it has to keep track of the allocated size and the filled size (tuples only need to store one size, because allocated and filled size are always identical). That means each list has to store another "size" which on 64bit systems is a 64bit integer, again 8 bytes.

So lists need at least 16 bytes more memory than tuples. Because of the over-allocation. Over-allocation means it allocates more space than needed. However, the amount of over-allocation depends on "how" you create the list and the append/deletion history:

>>> l = [1,2,3]
>>> l.__sizeof__()
64
>>> l.append(4)  # triggers re-allocation (with over-allocation), because the original list is full
>>> l.__sizeof__()
96

>>> l = []
>>> l.__sizeof__()
40
>>> l.append(1)  # re-allocation with over-allocation
>>> l.__sizeof__()
72
>>> l.append(2)  # no re-alloc
>>> l.append(3)  # no re-alloc
>>> l.__sizeof__()
72
>>> l.append(4)  # still has room, so no over-allocation needed (yet)
>>> l.__sizeof__()
72

Some handy links on this topic:

  • tuple struct
  • list struct

Python News Digest 8 - 14.02

Learn about the best Python tools, why sys.getsizeof is not what you need, how to call await on multiple functions and more
14 February 2020   239

Greetings! I hope your week went great! Here's new Python news digest.

Learn how parallelism can slow down your Python code, implementing interface in Python, how to check if a file is a valid image with Python and other intersting things awaits for you in this digest.

Articles

  • Understanding Best Practice Python Tooling by Comparing Popular Project Templates

Author checked and compare the most popular Python tools in this big article

  • The Parallelism Blues: when faster code is slower

Learn when, why and how parallelism can slow down your Python app

  • sys.getsizeof is not what you want

Learn why sys.getsizeof counts not all the bytes, and even wrong bytes

Guides

  • Implementing an Interface in Python

Tutorial for beginners on how to use a Python interface; understand why interfaces are so useful and learn how to implement formal and informal interfaces in Python

  • Python asyncio and await'ing multiple functions

In this tiny tutorial, you'll learn how to call await on multiple functions in Python using the asyncio package

  • How to Check if a File is a Valid Image with Python

Really small, but a useful tutorial that we will show you how to check if a certain file is a valied image using Python

  • Understand Group by in Django with SQL

Learn and understand what GROUP BY in Django ORM is by comparing QuerySets and SQL

Updates

  • virtualenv

A virtual environment builder for Python