Open In App

How to find the current capacity of a list - Python

Last Updated : 21 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

List in Python is mainly implementation of dynamic sized arrays (like ArrayList in Java or vector in C++). Capacity of a list means number of elements a list can store at a specific time. When we append an element to a list it will store the element if its size is less than capacity and if current capacity exceeds, then lists resize and allocate extra space for future insertions (Please see how lists work in Python for details). The formula for finding the capacity of a list and space left in the list: Here, size of an empty list means how many bits are allocated to an empty list and it varies system to system . size of one block of the list also varies from system to system. Length of the list means the number of blocks of space which is filled 

Example 1: 

Python
import sys
li = []
size = sys.getsizeof(li)
print("empty list size:", size)

# append an element in the list
li.append(1)

# size of the list after appending one element
print("size after appending:", sys.getsizeof(li))

# capacity of the list after appending one element
# Considering block size as 8.
capacity = (sys.getsizeof(li) - size) // 8
print("capacity:", capacity)
print("length:", len(li))

# space left in the list after appending one element
spaceleft = ((sys.getsizeof(li) - size) - len(li) * 8) // 8
print("space left:", spaceleft)

Output
size of an empty list: 56
total size of a list: 88
capacity of the list is: 4
length of the list is: 1
space left in the list is: 3

Example 2: 

Python
import sys
li = []
size = sys.getsizeof(l)
print("size of list", size)

# append four element in the list
li.append(1)
li.append(2)
li.append(3)
li.append(4)

# total size of the list after appending four element
print("size after appending", sys.getsizeof(li))

# capacity of the list after appending four element
c = (sys.getsizeof(l)-size)//8
print("capacity:", c)
print("length:", len(li))

# space left the list after appending four element
spaceleft =((sys.getsizeof(l)-size)-len(li)*8)//8
print("space left:", spaceleft)

Output
size of an empty list 56
Now total size of a list 88
capacity of the list is: 4
length of the list is: 4
space left in the list is: 0

Example 3: 

Python
import sys
li = []
size = sys.getsizeof(l)
print("empty list size:", size)

# append five element in the list
li.append(1)
li.append(2)
li.append(3)
li.append(4)
li.append(5)

# total size of the list after appending five element
print("size after appending ", sys.getsizeof(l))

# capacity of the list after appending five element
c = (sys.getsizeof(l)-size)//8
print("capacity:", c)
print("length:", len(l))

# space left the list after appending five element
spaceleft =((sys.getsizeof(l)-size)-len(l)*8)//8
print("space left:", spaceleft)

Output
size of an empty list : 56
Now total size of a list  120
capacity of the list is: 8
length of the list is 5
space left in the list is: 3

Next Article

Similar Reads