How to find the current capacity of a list in Python
Last Updated :
14 Mar, 2023
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 there is size is less than capacity. 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:
Python3
import sys
l = []
size = sys.getsizeof(l)
print ("size of an empty list :", size)
l.append( 1 )
print ("Now total size of a list :", sys.getsizeof(l))
capacity = (sys.getsizeof(l) - size) / / 8
print ("capacity of the list is :", capacity)
print ("length of the list is :", len (l))
spaceleft = ((sys.getsizeof(l) - size) - len (l) * 8 ) / / 8
print ("space left in the list is :", spaceleft)
|
OUTPUT
size of an empty list : 64
Now total size of a list : 96
capacity of the list is: 4
length of the list is: 1
space left in the list is: 3
Time complexity: O(1)
Auxiliary space: O(1)
Example 2:
Python3
import sys
l = []
size = sys.getsizeof(l)
print ("size of an empty list :", size)
l.append( 1 )
l.append( 2 )
l.append( 3 )
l.append( 4 )
print ("Now total size of a list :", sys.getsizeof(l))
c = (sys.getsizeof(l) - size) / / 8
print ("capacity of the list is :", c)
print ("length of the list is :", len (l))
spaceleft = ((sys.getsizeof(l) - size) - len (l) * 8 ) / / 8
print ("space left in the list is :", spaceleft)
|
OUTPUT
size of an empty list : 64
Now total size of a list : 96
capacity of the list is: 4
length of the list is: 4
space left in the list is: 0
The time complexity of this program is constant or O(1), meaning that the amount of time required to run the program remains the same, regardless of the size of the input.
The program uses a constant amount of auxiliary space to store the list and the size variable. Therefore, the auxiliary space complexity of this program is O(1), which is constant.
Example 3:
Python3
import sys
l = []
size = sys.getsizeof(l)
print ("size of an empty list :", size)
l.append( 1 )
l.append( 2 )
l.append( 3 )
l.append( 4 )
l.append( 5 )
print ("Now total size of a list :", sys.getsizeof(l))
c = (sys.getsizeof(l) - size) / / 8
print ("capacity of the list is :", c)
print ("length of the list is :", len (l))
spaceleft = ((sys.getsizeof(l) - size) - len (l) * 8 ) / / 8
print ("space left in the list is :", spaceleft)
|
OUTPUT
size of an empty list : 64
Now total size of a list : 128
capacity of the list is: 8
length of the list is: 5
space left in the list is: 3
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...