The complexity of in depends entirely on what L is. e in L will become L.__contains__(e).

See this time complexity document for the complexity of several built-in types.

Here is the summary for in:

  • list - Average: O(n)
  • set/dict - Average: O(1), Worst: O(n)

The O(n) worst case for sets and dicts is very uncommon, but it can happen if __hash__ is implemented poorly. This only happens if everything in your set has the same hash value.

Answer from Andrew Clark on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › time-complexity-of-in-operator-in-python
Time Complexity of In Operator in Python - GeeksforGeeks
July 23, 2025 - When you use the in operator to check if an element exists in a list, Python performs a linear search which means it iterates through each element of the list until it finds a match or reaches the end of the list. ... The time complexity is O(n) ...
Discussions

What is the time complexity of the “in” operation
This moved much faster. Because it does fewer tests - if you're checking for membership in a list, you can stop as soon as you find the element. If you're comparing every element of the list to a value, as the first example does, then you check every element of the list. Doing less is always faster. More on reddit.com
🌐 r/learnpython
10
2
September 1, 2021
algorithm - Complexity of creating list with concat operator in python - Stack Overflow
I am starting to learn about data structures+algorithms, and I have encountered an issue. Here is the function I am testing: def create_list_with_concat(n): l = [] for i in range(n): ... More on stackoverflow.com
🌐 stackoverflow.com
Time complexity of Counters in Python
And if so, wouldn't the time complexity of checking if a key is in a dictionary be O(n) Your link says O(1) on average, assuming a good enough hash function. More on reddit.com
🌐 r/learnpython
10
5
February 21, 2022
What is the time complexity of slicing a list?
O(n) where n is the length of the slice. Slicing is just "copy part of the list" so time complexity is the same as copy. More on reddit.com
🌐 r/learnpython
13
7
January 4, 2021
🌐
UCI
ics.uci.edu › ~pattis › ICS-33 › lectures › complexitypython.txt
Complexity of Python Operations
This change will speed up the code, but it won't change the complexity analysis because O(N + N Log N) = O (N Log N). Speeding up code is always good, but finding an algorithm in a better complexity class (as we did going from is_unique1 to is_unique2) is much better Finally, is_unique2 works ...
🌐
Reddit
reddit.com › r/learnpython › what is the time complexity of the “in” operation
r/learnpython on Reddit: What is the time complexity of the “in” operation
September 1, 2021 -

I’m not the biggest python user. But I was looking at a friends code yesterday and they had something like:

For x in (list of 40000)

For y in (list of 2.7 million)

  If x = y 

     Append something 

This was obviously super slow so they changed it to something like:

For x in (list of 2.7 million)

If y in (list of 40000)

  Append something 

This moved much faster. I get the point of one for loop being faster than two, but what is that “in” exists function doing that makes it so much faster. I always thought that to check if something exists is O(n) which shouldn’t be faster. Also this was for ML purposes so they were likely using numpy stuff.

🌐
Bradfield CS
bradfieldcs.com › algos › analysis › performance-of-python-types
Performance of Python Types
One decision they made was to optimize the list implementation for common operations. Two common operations are indexing and assigning to an index position. In Python lists, values are assigned to and retrieved from specific, known memory locations.
🌐
Cdotson
cdotson.com › 2015 › 07 › python-in-operator-theoretical-vs-actual-time-complexity
The Python “in” Operator – Theoretical vs Actual Time Complexity – Chad Dotson
Python has published the expected time complexities of their collection types. I’ve copied the ones for the in operator below. These Big-O numbers are exactly what you would expect since everything but a list is implemented using a hashing algorithm.
Find elsewhere
🌐
Quora
quora.com › What-are-the-time-complexity-considerations-of-lists-in-Python
What are the time complexity considerations of lists in Python? - Quora
Python lists are dynamic arrays (CPython implementation). Time complexity for common operations follows predictable amortized and worst-case patterns.
🌐
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 - Inserting or deleting an element at a specific index in a Python list requires shifting elements to accommodate the change. As a result, these operations have a linear time complexity of O(n).
🌐
GeeksforGeeks
geeksforgeeks.org › complexity-cheat-sheet-for-python-operations
Complexity Cheat Sheet for Python Operations - GeeksforGeeks
December 13, 2024 - Worst-case complexity arises when all elements are in a single hash bucket. ... Both are immutable, making operations like insertion or deletion invalid. Access and iteration are linear due to sequential traversal requirements. ... In Python, lists are one of the most commonly used data structures.
🌐
DEV Community
dev.to › iihsan › time-complexity-analysis-of-python-methods-bigo-notations-for-list-tuple-set-and-dictionary-methods-47l9
Time Complexity Analysis of Python Methods: Big(O) Notations for List, Tuple, Set, and Dictionary Methods - DEV Community
January 15, 2024 - So, we refer to it as Amortized constant time. The IN Operator uses linear search with a time complexity of O(n). Thanks for reading! Feel free to like, comment, and share if you find this article valuable.
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.

🌐
GitHub
gist.github.com › Gr1N › 60b346b5e91babb5efac
Complexity of Python Operations · GitHub
Save Gr1N/60b346b5e91babb5efac to your computer and use it in GitHub Desktop. Download ZIP · Complexity of Python Operations · Raw · complexitypython.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity - Python Wiki
n - k elements have to be moved, ... 1 moves. The average case for an average value of k is popping the element the middle of the list, which takes O(n/2) = O(n) operations....
🌐
Finxter
blog.finxter.com › home › learn python blog › complexity of python operations
Complexity of Python Operations - Be on the Right Side of Change
May 29, 2020 - Let’s explore the complexity of Python operations—classified by the data structure on which you perform those operations. A great coder will always use the data structure that suits their needs best. In general, the list data structure supports more operations than the set data structure as it keeps information about the ordering of the elements—at the cost of higher computational complexity.
🌐
GeeksforGeeks
geeksforgeeks.org › space-complexity-of-list-operations-in-python
Space Complexity of List Operations in Python - GeeksforGeeks
March 19, 2025 - Space Complexity: O(n), where n is the number of elements in the list. This is because shifting the elements to the right to make space for the new element requires memory for all affected elements. remove() removes the first occurrence of an ...
🌐
Python Morsels
pythonmorsels.com › time-complexities
Python Big O: the time complexities of different data structures in Python - Python Morsels
April 16, 2024 - For inexpensive operations involving the least-recently added item (the beginning of a list), we'd need a queue-like structure. That's what Python's collections.deque data structure is for. >>> from collections import deque >>> queue = deque([2, 1, 3, 4]) Here are the time complexities of common deque operations:
🌐
Quora
quora.com › How-fast-are-list-operations-in-Python
How fast are list operations in Python? - Quora
Answer (1 of 3): I went ahead and analyzed the runtime-complexities of some common list operations. Disclaimer: I have confidence in these complexities, but use at your own risk, etc. I welcome corrections, and have some questions about the implementation (see the end of the post). http://svn....
🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds3 › AlgorithmAnalysis › Lists.html
2.6. Lists — Problem Solving with Algorithms and Data Structures 3rd edition
Now that we have seen how performance can be measured concretely, you can look at Table 2 to see the Big O efficiency of all the basic list operations. After thinking carefully about Table 2, you may be wondering about the two different times for pop. When pop is called on the end of the list it takes \(O(1)\), but when pop is called on the first element in the list—or anywhere in the middle it—is \(O(n)\) The reason for this lies in how Python chooses to implement lists.
🌐
Intellipaat
intellipaat.com › community › 77012 › complexity-of-in-operator-in-python
Complexity of *in* operator in Python - Intellipaat Community
June 15, 2023 - What is the Complexity of the in administrator in Python? Is it theta(n)? Is it equivalent to the ... True return False Here, L is a list.