In this article, we’ll explore the space complexity of various list functions in Python, including common operations such as appending, inserting, deleting, and copying lists. Understanding the space complexity of these functions will help you make informed decisions when designing algorithms.
Related Articles: Complexity Cheat Sheet for Python List, sets, and Dict Operations
The Space Complexity of List Operations
Here are some of the main methods for working with lists in Python with some of their complexities.
Python append()
This method adds an element to the end of a list. The append() method will not result in significant additional memory overhead, as it simply adds the new element to the end of the existing list. The space complexity of this method is O(1).
Python3
my_list = [ 1 , 2 , 3 ] my_list.append( 4 ) print (my_list) |
Output :
[1, 2, 3, 4]
Python extend()
Extend() adds multiple elements to the end of a list. If you are extending a list with another list using extend(), the space complexity is O(n), where n is the length of the list being appended. This is because extend() iterates over the elements of the list being appended and adds them one by one to the end of the original list, resulting in a linear increase in memory usage with the size of the list being appended.
Python3
my_list = [ 1 , 2 , 3 ] my_list.extend([ 4 , 5 , 6 ]) print (my_list) |
Output :
[1, 2, 3, 4, 5, 6]
Python insert()
Insert() inserts an element at a specified position in the list. The space complexity of this method is O(1).
Python3
my_list = [ 1 , 2 , 3 ] my_list.insert( 1 , 0 ) print (my_list) |
Output :
[1, 0, 2, 3]
Python remove()
Remove() removes the first occurrence of an element from the list. The space complexity of this method is O(1) because it only requires deallocating memory for the removed element.
Python3
my_list = [ 1 , 2 , 3 ] my_list.remove( 2 ) print (my_list) |
Output :
[1, 3]
Python pop()
Pop() removes an element from a specified position in the list and returns it. The space complexity of this method is O(1) because it only requires deallocating memory for the removed element.
Python3
my_list = [ 1 , 2 , 3 ] my_list.pop( 1 ) print (my_list) |
Output :
[1, 3]
Python index()
Index() returns the index of the first occurrence of an element in the list. The space complexity of this method is O(1) because it only requires accessing the index of the element.
Python3
my_list = [ 1 , 2 , 3 ] index = my_list.index( 2 ) print (my_list) |
Output :
[1, 2, 3]
Python sort()
Sort() sorts the elements in a list in ascending order. The CPython implementation of Python, which is the default and most widely used implementation, uses a sorting algorithm called Timsort. Timsort in CPython is generally proportional to the size of the list sorted, which means that the space complexity of the sort() function in CPython can be considered as O(n), where n is the number of elements in the list sorted.
Python3
my_list = [ 3 , 2 , 1 ] my_list.sort() print (my_list) |
Output :
[1, 2, 3]
Python reverse()
Reverse() reverses the order of the elements in a list. The space complexity of this method is O(1) because it only requires swapping the positions of existing elements in the list.
Python3
my_list = [ 1 , 2 , 3 ] my_list.reverse() print (my_list) |
Output :
[3, 2, 1]