list

The Average Case assumes parameters generated uniformly at random.

Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.

So inserting an element at given position will always have the time complexity of O(n) as both insert method and slicing has time complexity of O(n) and O(k). Only append which inserts at the end of list have O(1) time complexity. From Python Wiki

Lists:
                               Complexity
Operation     | Example      | Class         | Notes
--------------+--------------+---------------+-------------------------------
Index         | l[i]         | O(1)          |
Store         | l[i] = 0     | O(1)          |
Length        | len(l)       | O(1)          |
Append        | l.append(5)  | O(1)          |
Clear         | l.clear()    | O(1)          | similar to l = []

Slice         | l[a:b]       | O(b-a)        | l[1:5]:O(l)/l[:]:O(len(l)-0)=O(N)
Extend        | l.extend(...)| O(len(...))   | depends only on len of extension
Construction  | list(...)    | len(...)      | depends on lenghth of argument

check ==, !=  | l1 == l2     | O(N)          |
Insert        | l[a:b] = ... | O(N)          |
Delete        | del l[i]     | O(N)          | 
Remove        | l.remove(...)| O(N)          | 
Containment   | x in/not in l| O(N)          | searches list
Copy          | l.copy()     | O(N)          | Same as l[:] which is O(N)
Pop           | l.pop(...)   | O(N)          |
Pop           | l.pop()      | O(1)          | same as l.pop(-1), popping at end
Extreme value | min(l)/max(l)| O(N)          |
Reverse       | l.reverse()  | O(N)          |
Iteration     | for v in l:  | O(N)          |

Sort          | l.sort()     | O(N Log N)    | key/reverse doesn't change this
Multiply      | k*l          | O(k N)        | 5*l is O(N): len(l)*l is O(N**2)

From here

Answer from Tanveer Alam on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › what is the time complexity (big o) for insertion of an element in the end of an array?
r/learnpython on Reddit: What is the Time complexity (Big O) for insertion of an element in the end of an Array?
January 3, 2021 -

Assume I have a code:

arr1=array("i", [1, 2, 3, 4, 5])

I just wanted to ask what would be the time complexity for inserting an element at the end of the array by using the code:

arr1.insert(5,6)

output: array('i', [1, 2, 3, 4, 5, 6])

Although, I found online that Big O is O(1) but I don't get it. Initially my array was of fixed sized(5) and if I need to add a new element, I am essentially copying the old array [1,2,3,4,5] and pasting it in a new memory location with an empty space at the end so that I could add [6] in the end. This is my understanding, if this is right how is O(1) correct? shouldn't it be more?

Edit: It's O(n)

Top answer
1 of 4
65

list

The Average Case assumes parameters generated uniformly at random.

Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.

So inserting an element at given position will always have the time complexity of O(n) as both insert method and slicing has time complexity of O(n) and O(k). Only append which inserts at the end of list have O(1) time complexity. From Python Wiki

Lists:
                               Complexity
Operation     | Example      | Class         | Notes
--------------+--------------+---------------+-------------------------------
Index         | l[i]         | O(1)          |
Store         | l[i] = 0     | O(1)          |
Length        | len(l)       | O(1)          |
Append        | l.append(5)  | O(1)          |
Clear         | l.clear()    | O(1)          | similar to l = []

Slice         | l[a:b]       | O(b-a)        | l[1:5]:O(l)/l[:]:O(len(l)-0)=O(N)
Extend        | l.extend(...)| O(len(...))   | depends only on len of extension
Construction  | list(...)    | len(...)      | depends on lenghth of argument

check ==, !=  | l1 == l2     | O(N)          |
Insert        | l[a:b] = ... | O(N)          |
Delete        | del l[i]     | O(N)          | 
Remove        | l.remove(...)| O(N)          | 
Containment   | x in/not in l| O(N)          | searches list
Copy          | l.copy()     | O(N)          | Same as l[:] which is O(N)
Pop           | l.pop(...)   | O(N)          |
Pop           | l.pop()      | O(1)          | same as l.pop(-1), popping at end
Extreme value | min(l)/max(l)| O(N)          |
Reverse       | l.reverse()  | O(N)          |
Iteration     | for v in l:  | O(N)          |

Sort          | l.sort()     | O(N Log N)    | key/reverse doesn't change this
Multiply      | k*l          | O(k N)        | 5*l is O(N): len(l)*l is O(N**2)

From here

2 of 4
31

The Python language doesn't specify the implementation of such operations, so different implementations may have different behavior. For CPython, the complexity of list.insert is O(n), as shown on this useful wiki page. I'm not aware of any list-like structure giving O(1) performance for inserting at an arbitrary index. (A dict gives O(1) insert performance in the average case, but is not ordered and does not enforce a contiguous sequence of indices.) The blist library provides an optimized list type that has an O(log n) insert.

Discussions

list - Is a.insert(0,x) an o(n) function? Is a.append an O(1) function? Python - Stack Overflow
I am trying to move even numbers in an array to the front and odd numbers to the back of the array. The problem asks to do this in a Linear Algorithm and do this In Place. I came up with this: def More on stackoverflow.com
🌐 stackoverflow.com
What is Python's list.append() method WORST Time ...
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
🌐 r/learnpython
What is the Time complexity (Big O) for insertion of an element in the end of an Array?
Python lists and arrays preallocate extra memory so that you can append to them as O(1). In that sense they are not real arrays like we are taught. More on reddit.com
🌐 r/learnpython
9
1
January 3, 2021
What is Python's list.append() method WORST Time Complexity? It can't be O(1), right?
Personally, I thought that lists worked as double LinkedLists, so insert time was O(1) But if it works as a dynamic array, time complexity should be amortized time, ie, close to O(1) but not quite More on reddit.com
🌐 r/learnpython
11
3
October 26, 2022
🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity
Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move).
🌐
Medium
medium.com › @ivanmarkeyev › understanding-python-list-operations-a-big-o-complexity-guide-49be9c00afb4
Understanding Python List Operations: A Big O Complexity Guide | by Ivan Markeev | Medium
June 4, 2023 - It’s important to note that these time complexities are average cases, and they may vary depending on factors such as the size of the list, the specific operation being performed, and the underlying implementation of the Python interpreter.
🌐
Python Morsels
pythonmorsels.com › time-complexities
Python Big O: the time complexities of different data structures in Python - Python Morsels
April 16, 2024 - Thanks to the power of hashing, ... the value of an item, and removing an item are all constant time operations (that's O(1) in big O)....
🌐
GeeksforGeeks
geeksforgeeks.org › python › time-complexity-for-adding-element-in-python-set-vs-list
Time Complexity for Adding Element in Python Set vs List - GeeksforGeeks
July 23, 2025 - When we add an element to a list using the append() method, Python directly adds the element to the end. This operation has O(1) amortized time complexity, as no hashing or duplicate checks are needed.
🌐
Facebook
facebook.com › fb-answers › python-list-insert-complexity-official-documentation
Python List Insert Complexity and Big O Notation
Explore the things you love · Log into Facebook · English (US) · فارسی · العربية · Türkçe · Deutsch · Français (France) · Polski · More languages…
Find elsewhere
🌐
edSlash
edslash.com › home › python tutorials – learn with edslash › insert() method in list – python
insert() method in list - Python - edSlash
September 21, 2024 - The .insert() method takes O(n) time complexity where n is the number of elements.
🌐
Medium
medium.com › @abbasi.alain › data-structure-and-time-space-complexity-in-python-for-datascientist-55d8a2c8805f
Essential Data Structures and Time/Space complexity in Python | by Dr. Alain ABBASI (Ph.D.) | Medium
September 25, 2024 - Heaps are binary trees that maintain ... element. Python Library: The built-in heapq module can be used to implement heaps with O(log n) time complexity for insertion and deletion....
🌐
GitHub
github.com › pberkes › big_O
GitHub - pberkes/big_O: Python module to estimate big-O time complexity from execution time · GitHub
>>> import big_o >>> positive_int_generator = lambda n: big_o.datagen.integers(n, 0, 10000) >>> best, others = big_o.big_o(find_max, positive_int_generator, n_repeats=100) >>> print(best) Linear: time = -0.00035 + 2.7E-06*n (sec) big_o inferred that the asymptotic behavior of the find_max function is linear, and returns an object containing the fitted coefficients for the complexity class.
Author: pberkes
🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds3 › AlgorithmAnalysis › Lists.html
2.6. Lists — Problem Solving with Algorithms and Data Structures 3rd edition
To use timeit you create a Timer object whose parameters are two Python statements. The first parameter is a Python statement that you want to time; the second parameter is a statement that will run once to set up the test. The timeit module will then time how long it takes to execute the statement ...
🌐
Coding Confessions
blog.codingconfessions.com › confessions of a code addict › looking under the hood of python's set data structure
Looking Under the Hood of Python's Set Data Structure
October 14, 2024 - And, in the case of data structures, ... and time complexity, you also have to be aware of the implementation trade-offs, such as memory overhead, concurrency, and cpu cache friendliness. In this post, I want to do this analysis for Python’s set implementation. We are going to discuss its data model and also discuss the implementation of the key apis: insertion, lookups, ...
🌐
Wikipedia
en.wikipedia.org › wiki › Insertion_sort
Insertion sort - Wikipedia
1 week ago - Adaptive, i.e., efficient for data ... the time complexity is O(kn) when each element in the input is no more than k places away from its sorted position · Stable; i.e., does not change the relative order of elements with equal keys · In-place; i.e., only requires a constant amount O(1) of additional memory space ... When people manually sort cards in a bridge hand, most use a method that is similar to insertion ...
🌐
Medium
medium.com › @Mandeep2002 › time-complexity-of-in-built-python-data-structures-98807ab1250e
Time Complexity of In-built Python Data Structures | by Mandeep Singh Saluja | Medium
July 9, 2024 - Time Complexity · Python · Mandeep Singh Saluja · 2 min read · ·Jul 1, 2024 · -- Listen · Share · Dictionaries in Python are implemented as hash tables. The efficiency of dictionary operations comes from the use of hashing. Access: O(1) Accessing a value by its key involves computing the hash of the key and looking up the value in the hash table. This is generally O(1) due to the direct index lookup. Insert: O(1) Inserting a new key-value pair involves computing the hash of the key and placing the value in the corresponding bucket.
🌐
Favtutor
favtutor.com › articles › edit-distance-problem
Edit Distance Problem (C++, Java, Python)
March 2, 2024 - The space complexity for the above code is O(1). In the above approach, we observe that we are computing the minimum operations by performing all three operations on each index. This leads to a high time complexity as we might compute the same values repeatedly.
🌐
Hashnode
nagsblog.hashnode.dev › data-structures-and-algorithms-1
Search, Insert, and Delete in an Unsorted Array
August 24, 2024 - Here’s a detailed explanation of the search, insert, and delete operations in an unsorted array, along with their time and space complexities, and implementations in C++, Java, and Python. 1. Search Operation Algorithm: Iterate through each element ...