Based on this older thread, it looks like the cost of list functions in Python is:
Can anyone confirm whether this is still true in Python 2.6/3.x?
According to Python's official Time Complexity page1, using list. insert always has O(n) (linear) complexity.
Python List insert() Time Complexity. The time complexity to insert an element at a given index is O(n). This means that the complexity increases linearly with the number of elements in the list.
Take a look here. It's a PEP for a different kind of list. The version specified is 2.6/3.0.
Append (insertion to back) is O(1)
, while insertion (everywhere else) is O(n)
. So yes, it looks like this is still true.
Operation...Complexity
Copy........O(n)
Append......O(1)
Insert......O(n)
Get Item....O(1)
Set Item....O(1)
Del Item....O(n)
Iteration...O(n)
Get Slice...O(k)
Del Slice...O(n)
Set Slice...O(n+k)
Extend......O(k)
Sort........O(n log n)
Multiply....O(nk)
Python 3 is mostly an evolutionary change, no big changes in the datastructures and their complexities.
The canonical source for Python collections is TimeComplexity on the Wiki.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With