Get item is getting an item in a specific index, while lookup means searching if some element exists in the list. To do so, unless the list is sorted, you will need to iterate all elements, and have O(n) Get Item operations, which leads to O(n) lookup.

A dictionary is maintaining a smart data structure (hash table) under the hood, so you will not need to query O(n) times to find if the element exists, but a constant number of times (average case), leading to O(1) lookup.

Answer from amit on Stack Overflow
๐ŸŒ
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 - Under the hood, lists use an underlying array structure to store their elements. This enables direct access to any element by index, resulting in O(1) complexity. Regardless of the size of the list, accessing an element takes the same amount of time.
Discussions

How are list elements accessed in python internally? - Software Engineering Stack Exchange
So my question is how does python get access to a particular index position in lists? Is the time complexity O(1) like in C?? More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
May 13, 2018
What is the time complexity of the โ€œinโ€ operation
It's not operator-specific, the time complexity depends entirely on how the object implements its __contains__-method. For lists, the time complexity is O(n). But for a set or dictionary it would be O(1). More on reddit.com
๐ŸŒ r/learnpython
10
2
September 1, 2021
Internals of Python list, access and resizing runtimes - Stack Overflow
Does a Python tuple have faster access times than a python list? ... "I read here that array access is really slow in Python." - This was very out of date even when this question was originally asked. It's also talking about constant-factor overhead, while the question here is about big-O complexity... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Time complexity of printing a list? The entire list object itself, not its individual elements (Python)
The question "Time Complexity of printing a list" has got an accepted answer by mhawke with the score of 4: The output is different, but that's not important. Either method is O(n). In the first case each element of the list is visited once hence O(n). In the second case the print() function itself iterates over the list visiting each element once, also O(n). If you really want to you can read the code in the Python source code repository. This action was performed automagically. info_post Did I make a mistake? contact or reply: error More on reddit.com
๐ŸŒ r/AskProgramming
39
11
September 7, 2021
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ TimeComplexity
TimeComplexity - Python Wiki
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).
๐ŸŒ
YourBasic
yourbasic.org โ€บ algorithms โ€บ time-complexity-arrays
Time complexity of array/list operations [Java, Python] ยท YourBasic
The worst-case time complexity is linear. Similarly, searching for an element for an element can be expensive, since you may need to scan the entire array. In this Python code example, the linear-time pop(0) call, which deletes the first element of a list, leads to highly inefficient code:
๐ŸŒ
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.

Top answer
1 of 4
47

Python's [] is implemented as an array, not a linked list. Although resizing is O(n), appending to it is amortized O(1), because resizes happen very rarely. If you're not familiar with how this works, read this Wikipedia entry on dynamic arrays. Python's list doesn't expand by a factor of 2 each time, it's a bit more complicated than that, but the expansions are still designed to make appending amortized O(1).

Inserting in the middle, however, is always an inefficient O(n), because n items may have to be moved.

Tuples aren't faster than lists - they're just immutable lists under the hood (*).

Regarding your dictionary test: depending on your exact implementation, caching in a list will be faster than with a dict. However, Python's dicts are highly optimized, and especially for small amounts of keys will perform great.


(*) Here's a list's "get item" C function in Python 2.6:

PyObject *
PyList_GetItem(PyObject *op, Py_ssize_t i)
{
    if (!PyList_Check(op)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    if (i < 0 || i >= Py_SIZE(op)) {
        if (indexerr == NULL)
            indexerr = PyString_FromString(
                "list index out of range");
        PyErr_SetObject(PyExc_IndexError, indexerr);
        return NULL;
    }
    return ((PyListObject *)op) -> ob_item[i];
}

And this is a tuple's:

PyObject *
PyTuple_GetItem(register PyObject *op, register Py_ssize_t i)
{
    if (!PyTuple_Check(op)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    if (i < 0 || i >= Py_SIZE(op)) {
        PyErr_SetString(PyExc_IndexError, "tuple index out of range");
        return NULL;
    }
    return ((PyTupleObject *)op) -> ob_item[i];
}

As you can see, they're almost exactly the same. In the end, after some type and bound checking, it's a simple pointer access with an index.

[Reference: Python documentation on Time Complexity for data type operations]

2 of 4
9

There is a great list here outlining the time complexity of the python data types. In your case item retrieval should be O(1) time.

Find elsewhere
๐ŸŒ
Bradfield CS
bradfieldcs.com โ€บ algos โ€บ analysis โ€บ performance-of-python-types
Performance of Python Types
Slice operations require more thought. To access the slice [a:b] of a list, we must iterate over every element between indices a and b. So, slice access is ... O(n)O(n) since we must reposition each element. Finally (and least intuitively), sorting in Python is
๐ŸŒ
Quora
quora.com โ€บ How-do-Python-lists-maintain-constant-time-complexity-for-indexing-if-their-elements-can-be-of-more-than-one-type
How do Python lists maintain constant time complexity for indexing if their elements can be of more than one type? - Quora
Answer (1 of 4): in a C arrays where the data is held in contiguous memory, you are right that indexing couldnโ€™t be constant time in a heterogeneous container as you would have to sum the widths of all of the previous items before being able to fetch an item (or you would need to keep a separate ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ complexity-cheat-sheet-for-python-operations
Complexity Cheat Sheet for Python Operations - GeeksforGeeks
July 12, 2025 - This cheat sheet is designed to ... complexities of common operations for these data structures that help them write optimized and efficient code in Python. Python's list is an ordered, mutable sequence, often implemented as a dynamic array. Below are the time complexities ...
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ time-complexities
Python Big O: the time complexities of different data structures in Python - Python Morsels
April 16, 2024 - Python's lists are similar to arrays or array lists in some other languages. Here are the time complexities of some common list operations:
๐ŸŒ
DEV Community
dev.to โ€บ williams-37 โ€บ understanding-time-complexity-in-python-functions-5ehi
Understanding Time Complexity in Python Functions - DEV Community
October 25, 2024 - It is usually expressed using Big O notation, which classifies algorithms according to their worst-case or upper bound performance. Common time complexities include: ... Understanding these complexities helps developers choose the right algorithms and data structures for their applications. ... Accessing an element by index in a list is a constant time operation.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ how can i manipulate python list elements using indexing?
How can I Manipulate Python List Elements Using Indexing?
January 22, 2024 - Direct indexing has a time complexity of O(1), while using the index() method for searching has a time complexity of O(n). Opting for direct indexing can significantly improve efficiency for frequent index-based operations.
๐ŸŒ
Reddit
reddit.com โ€บ r/askprogramming โ€บ time complexity of printing a list? the entire list object itself, not its individual elements (python)
r/AskProgramming on Reddit: Time complexity of printing a list? The entire list object itself, not its individual elements (Python)
September 7, 2021 -

I've been trying to Google this for hours, but only found one that answers it. However the explanation was short, and because there was only one, I couldn't make sure if it was correct at all.

Let's say a function asks for an input n, and then it creates a list x with n elements. What would then be the time complexity of print(x) with respect to n? Would its run time scale with whatever the size n of the list x is and therefore it would be O(n)? Or is its runtime going to be constant regardless of the size n?

The reason I'm having a hard time finding an answer to this in Google is because the results I get is always talking about iterating through the list and printing each element individually, instead of printing the list object itself.

According to the one result I found, it is also O(n), because the print function in this case apparently also iterates over the list visiting each element once. It would be nice if anyone else could confirm if this is correct.

๐ŸŒ
Quora
quora.com โ€บ What-are-the-time-complexity-considerations-of-lists-in-Python
What are the time complexity considerations of lists in Python? - Quora
Answer: In a normal list on average: * Append : O(1) * Extend : O(k) - k is the length of the extension * Index : O(1) * Slice : O(k) * Sort : O(n log n) - n is the length of the list * Len : O(1) * Pop : O(1) - pop from end * Insert : O(n) - n is the length of the list * Del : O(n) - n...
๐ŸŒ
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 - ... Appending an element to the end of the list is O(1) on average. Occasionally, the list may need to resize, which involves copying elements to a new larger array, making the worst-case complexity O(n).
๐ŸŒ
Devcuriosity
devcuriosity.com โ€บ blog โ€บ details โ€บ arrays-computer-science-python-overview
Arrays in Computer Science and Python - An overview with space and time complexities
This operation has a time complexity of O(n). Where n is, of course, the number of elements in the list. Lists in Python are mutable (really important!). It is also worth noting that regular arrays (in languages like C) are storing values directly. Python is storing in the list a references ...