You are creating a new list object each time by concatenating. This requires copying all elements from the old list into a new one, plus one extra. So yes, using l = l + [i] is an O(N) algorithm, not O(1).

At the very least, don't use + concatenation; use += augmented concatenation, which is the same thing as list.extend() with a re-assignment to the same reference:

def test3():
    l = []
    for i in range(1000):
        l += [i]  # or use l.extend([i])
    return l

This produces:

>>> print timeit.repeat(stmt=test1, number=100, repeat=2)
[0.1333179473876953, 0.12804388999938965]
>>> print timeit.repeat(stmt=test2, number=100, repeat=2)
[0.01052403450012207, 0.007989168167114258]
>>> print timeit.repeat(stmt=test3, number=100, repeat=2)
[0.013209104537963867, 0.011193037033081055]
Answer from Martijn Pieters on Stack Overflow
Top answer
1 of 3
11

The time complexity is not O(N)

The time complexity of the concat operation for two lists, A and B, is O(A + B). This is because you aren't adding to one list, but instead are creating a whole new list and populating it with elements from both A and B, requiring you to iterate through both.

Therefore, doing the operation l = l + [i] is O(len(l)), leaving you with N steps of doing an N operation, resulting in an overall complexity of O(N^2)

You are confusing concat with the append or extend function, which doesn't create a new list but adds to the original. If you used those functions, your time complexity would indeed be O(N)

An additional note:

The notation l = l + [i] can be confusing because intuitively it seems like [i] is simply being added to the existing l. This isn't true!

l + [i] builds a entirely new list and then has l point to that list.

On the other hand l += [i] modifies the original list and behaves like extend

2 of 3
3

Here is my thought process: I know that the concat operator is O(k) where k is the size of the list being added to the original list. Since the size of k is always 1 in this case because we are adding one character lists at a time, the concat operation takes 1 step.

This assumption is incorrect. If you write:

l + [i]

you construct a new list, this list will have m+1 elements, with m the number of elements in l, given a list is implemented like an array, we know that constructing such list will take O(m) time. We then assign the new list to l.

So that means that the total number of steps is:

 n
---
\              2
/    O(m) = O(n )
---
m=0

so the time complexity is O(n2).

You can however boost performance, by using l += [i], or even faster l.append(i), where the amortize cost is, for both l += [i] and l.append(i) O(1), so then the algorithm is O(n), the l.append(i) will however likely be a bit faster because we save on constructing a new list, etc.

Discussions

Runtime of merging two lists in Python - Stack Overflow
When you concatenate the two lists with A + B, you create a completely new list in memory. This means your guess is correct: the complexity is O(n + m) (where n and m are the lengths of the lists) since Python has to walk both lists in turn to build the new list. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Is the time-complexity of iterative string append actually O(n^2), or O(n)? - Stack Overflow
I am working on a problem out of CTCI. The third problem of chapter 1 has you take a string such as 'Mr John Smith ' and asks you to replace the intermediary spaces with : 'Mr John S... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Time complexity of appending a list to a list - Stack Overflow
I understand the amortized complexity of appending an element to a list is O(1) but what is the time complexity of appending a list to a list? For clarification: Appending an element to a list list... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Improve speed for list concatenation? - Stack Overflow
Only for really large strings you should avoid repeated concatenation. 2011-07-29T19:50:27.937Z+00:00 ... Well, the conclusion of this (and this stackoverflow.com/questions/376461/โ€ฆ) is that you should try the different solutions and benchmark them to find the most efficient solution in your case. :-). 2011-07-29T20:42:37.743Z+00:00 ... Since accessing an element in a python list is O(1), and appending a list to another is O(1) (which is probably the time complexity ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 29, 2011
๐ŸŒ
Farmsfields
farmsfields.com โ€บ list-concatenation-python-time-complexity
List Concatenation Python Time Complexity
May 5, 2025 - Reason Each concatenation creates a new list and copies all existing elements repeatedly. ... Time Complexity O(k), linear with the total number of elements. Reason In-place modification avoids repeated copying of existing elements.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ how to merge two lists in python?
How To Merge Two Lists in Python?
January 23, 2024 - Concatenated list using * operator: [โ€˜appleโ€™, โ€˜bananaโ€™, โ€˜orangeโ€™, โ€˜grapeโ€™, โ€˜watermelonโ€™, โ€˜kiwiโ€™] ... The time complexity of list concatenation using the * operator is O(n + m), where n and m are the lengths of the two ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python list concatenation time complexity
To learn more, please visit the YouTube Help Center: https://www.youtube.com/help
๐ŸŒ
AlgoMonster
algo.monster โ€บ liteproblems โ€บ 1929
1929. Concatenation of Array - In-Depth Explanation
The time complexity is O(n), where n is the length of the array nums. The concatenation operation nums + nums in Python creates a new list by iterating through all elements of the first nums array and then all elements of the second nums array, resulting in 2n operations, which simplifies to O(n).
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ pythonds3 โ€บ AlgorithmAnalysis โ€บ Lists.html
2.6. Lists โ€” Problem Solving with Algorithms and Data Structures 3rd edition
One final observation about this ... comparison of the operations. So it would not be accurate to say that the concatenation operation takes 6.54 milliseconds but rather the concatenation test function takes 6.54 milliseconds....
๐ŸŒ
Codeforces
codeforces.com โ€บ blog โ€บ entry โ€บ 125610
Optimize Your Python Codeforces Solutions: Say Goodbye to str += str and Time Limit - Codeforces
When building strings iteratively, it's a common instinct to use the += operator to concatenate strings. However, what many Python developers may not realize is that this operation has a time complexity of approximately O(n^2).
Find elsewhere
๐ŸŒ
w3reference
w3reference.com โ€บ blog โ€บ 6-ways-to-concatenate-lists-in-python
6+ Ways to Concatenate Lists in Python: A Comprehensive Guide โ€” w3reference.com
The + operator is the most ... 2, 3, 4, 5, 6] Creates a new list (does not modify the original lists) Time complexity: O(n + m) where n and m are the lengths of the lists...
Top answer
1 of 3
15

When you concatenate the two lists with A + B, you create a completely new list in memory. This means your guess is correct: the complexity is O(n + m) (where n and m are the lengths of the lists) since Python has to walk both lists in turn to build the new list.

You can see this happening in the list_concat function in the source code for Python lists:

static PyObject *
list_concat(PyListObject *a, PyObject *bb)
{
/* ...code snipped... */
    src = a->ob_item;
    dest = np->ob_item;
    for (i = 0; i < Py_SIZE(a); i++) {     /* walking list a */
        PyObject *v = src[i];
        Py_INCREF(v);
        dest[i] = v;
    }
    src = b->ob_item;
    dest = np->ob_item + Py_SIZE(a);
    for (i = 0; i < Py_SIZE(b); i++) {     /* walking list b */
        PyObject *v = src[i];
        Py_INCREF(v);
        dest[i] = v;
    }
/* ...code snipped... */

If you don't need a new list in memory, it's often a good idea to take advantage of the fact that lists are mutable (and this is where Python is smart). Using A.extend(B) is O(m) in complexity meaning that you avoid the overhead of copying list a.

The complexity of various list operations are listed here on the Python wiki.

2 of 3
2

My first guess is O(n+m), not sure if Python is smarter than that.

Nothing can be smarter than that while returning a copy. Though even if A, B were immutable sequences such as strings; CPython still makes a full copy instead of aliasing the same memory (it simplifies implementation of the garbage collection for such strings).

In some specific cases, the operation could be O(1) depending on what you want to do with the result e.g., itertools.chain(A, B) allows to iterate over all items (it does not make a copy, the change in A, B affects yielded items). Or if you need a random access; you could emulate it using a Sequence subclass e.g., WeightedPopulation but in the general case the copy and therefore O(n+m) runtime is unavoidable.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-records-list-concatenation
Python | Records List Concatenation - GeeksforGeeks
April 13, 2023 - The original list 1 : [('g', 'f'), ... 'isbest'), ('best', 'st gfg')] Time complexity: O(n), where n is the length of the shorter list between test_list1 and test_list2....
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ how-to-concatenate-list-in-python
How to Concatenate Two Lists in Python: 6 Effective Methods | Codecademy
For large lists or repeated concatenation, + and extend() are the most efficient. Between them, extend() is typically more efficient than + because + creates new lists with each operation, which increases memory usage and time complexity. Yes, Python lists can hold mixed data types.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-concatenate-n-consecutive-elements-in-string-list
Python | Concatenate N consecutive elements in String list - GeeksforGeeks
April 13, 2023 - Time complexity: O(n) Where n is the length of the input list test_list. In each recursive call, we slice the input list into two parts, which takes O(n) time in the worst case. We then make one recursive call with the second part of the list, ...
๐ŸŒ
Earthly
earthly.dev โ€บ blog โ€บ python-concatenate-lists
Python Concatenate Lists - Earthly Blog
July 14, 2023 - >>> setup = """\ from itertools import chain longlist = ["one","two", "three"] * 1000; nestedlist = [["one","two", "three"],["four","five"],[]] """ >>> run = """\ for item in nestedlist: longlist.extend(item) """ >>> timeit.timeit(run, setup=setup, number=100000) 0.02403609199973289 ยท There we go, extend is much faster when flattening lists or concatenating many lists with one long list. If you encounter this, using extend to add the smaller lists to the long list can decrease the work that has to be done and increase performance. These are the main variants of combining lists in python.
๐ŸŒ
Plain English
plainenglish.io โ€บ home โ€บ blog โ€บ python โ€บ how to efficiently concatenate strings in python
How To Efficiently Concatenate Strings In Python
November 24, 2020 - The reason for this is, on every โ€œ+โ€ operation a new string is created(Remember strings are immutable?) every single time and the existing values have to be copied character by character. The time complexity of this operation is O(nยฒ).
๐ŸŒ
DEV Community
dev.to โ€บ fayomihorace โ€บ python-how-simple-string-concatenation-can-kill-your-code-performance-2636
Python: How simple string concatenation can kill your code performance - DEV Community
March 18, 2023 - So because concatenation creates a new string of length len(s+c) , the complexity of the concatenation at each iteration is the length of s at the previous iteration + length of string c. As you can see the total, or overall complexity is 1 ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ concatenate-lists-python
6+ Ways to Concatenate Lists in Python | DigitalOcean
October 8, 2025 - However, note that using sum() for list concatenation is inefficient for large lists because it repeatedly creates new lists during the addition process, leading to quadratic time complexity.