It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity

This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)...

Answer from Zach Kelling on Stack Overflow
🌐
Bradfield CS
bradfieldcs.com › algos › analysis › performance-of-python-types
Performance of Python Types
In Python lists, values are assigned to and retrieved from specific, known memory locations. No matter how large the list is, index lookup and assignment take a constant amount of time and are thus
🌐
Stack Overflow
stackoverflow.com › tags › time-complexity › new
'time-complexity' New Answers - Stack Overflow
last element's index will be 39200 + 9 * 32 + 1 = 39 489. Generally, if you want to access ton`th element value the computer will read size_of_type_of_array bits starting from array_offset_in_memory + ... ... time-complexity × 10326 algorithm × 4868 big-o × 2337 python × 1379 java × 1317 complexity-theory × 1202 data-structures × 805 c++ × 776 arrays × 605 performance × 599 sorting × 550 recursion × 519 space-complexity × 487 c × 373 javascript × 300 math × 254 time × 235 python-3.x × 230 string × 212 list × 184 loops × 181 runtime × 162 c# × 156 recurrence × 156 optimization × 148
Find elsewhere
🌐
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 ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python list index() – a simple illustrated guide
Python List index() - A Simple Illustrated Guide - Be on the Right Side of Change
June 19, 2021 - For n elements, the runtime complexity is O(n) because in the worst-case you need to iterate over each element in the list to find that the element does not appear in it. Let’s check the runtime complexity practically for different list sizes ...
🌐
Stack Overflow
stackoverflow.com › questions › 79826728 › how-to-implement-a-list-with-an-efficient-index-of-operation
algorithm - How to implement a list with an efficient "index of" operation? - Stack Overflow
Acceptable time complexities include O(1) and O(log(N)). † Finding an index of a given value, assuming it's present in the list (even in a hypothetical case of a list implementation disallowing duplicates) in sub-linear time is, with all ...
🌐
Stack Overflow
stackoverflow.com › questions › 62028924 › whats-the-time-complexity-for-some-python-list-operations
What's the time complexity for some python list operations? - Stack Overflow
May 26, 2020 - The time complexity is O(n), which is approximately n/2. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking.
🌐
Stack Overflow
stackoverflow.com › questions › 21082323 › worst-case-time-complexity-lists
arrays - Worst case time complexity lists - Stack Overflow
It requires O(1) access to any index, that's simple with an array, but impossible with a list. Think of how you would pass from element n/2 to n/4 for e.g without traversing all the elements in the middle, or from the beginning. Insertion sort on the other hand is based on traversing the elements consecutively, which is not a problem in a linked list, and therefore will retain the same complexity.
Top answer
1 of 6
82

Information on this topic is now available on Wikipedia at: Search data structure

+----------------------+----------+------------+----------+--------------+
|                      |  Insert  |   Delete   |  Search  | Space Usage  |
+----------------------+----------+------------+----------+--------------+
| Unsorted array       | O(1)     | O(1)       | O(n)     | O(n)         |
| Value-indexed array  | O(1)     | O(1)       | O(1)     | O(n)         |
| Sorted array         | O(n)     | O(n)       | O(log n) | O(n)         |
| Unsorted linked list | O(1)*    | O(1)*      | O(n)     | O(n)         |
| Sorted linked list   | O(n)*    | O(1)*      | O(n)     | O(n)         |
| Balanced binary tree | O(log n) | O(log n)   | O(log n) | O(n)         |
| Heap                 | O(log n) | O(log n)** | O(n)     | O(n)         |
| Hash table           | O(1)     | O(1)       | O(1)     | O(n)         |
+----------------------+----------+------------+----------+--------------+

 * The cost to add or delete an element into a known location in the list 
   (i.e. if you have an iterator to the location) is O(1). If you don't 
   know the location, then you need to traverse the list to the location
   of deletion/insertion, which takes O(n) time. 

** The deletion cost is O(log n) for the minimum or maximum, O(n) for an
   arbitrary element.
2 of 6
17

I guess I will start you off with the time complexity of a linked list:

Indexing---->O(n)
Inserting / Deleting at end---->O(1) or O(n)
Inserting / Deleting in middle--->O(1) with iterator O(n) with out

The time complexity for the Inserting at the end depends if you have the location of the last node, if you do, it would be O(1) other wise you will have to search through the linked list and the time complexity would jump to O(n).

Top answer
1 of 3
3

There is no Java (standard) collection type with that property.

The indexOf(Object) method is declared in List, and there are no List implementations that provide better than O(N) lookup.

There are other collection types that provide O(logN) or O(1) lookup, but they do not return a position (and index) like indexOf(Object) does. (HashMap and HashSet provide respectively get(key) and contains(object) that are O(1).)


If there is nothing, can anyone help me out to deduce the reason why such has not been formed? Any particular reason?

Because a data structure that did provide such functionality would be complicated, and (probably) slower and/or combine the drawbacks of both an ArrayList and a HashMap.

I can't think of a good way to implement a list with fast lookup; i.e. one that didn't have serious drawbacks. And if there was a good way, I would have expected the Java team to know about it ... and to have included it in the Java SE library. This is similar to why there are no general purpose concurrent list types.

I am not saying that it is impossible. I'm saying that if it is possible, nobody has discovered a way to do it. AFAIK.

Designing an general purpose collection type involves compromise. When you optimize for one mode of access, you will often de-optimize for another.

2 of 3
0

To find the index of an element in a Collection in O(1) you need to be able to directly retrieve the index of the element without searching. A List or an array doesn't allow us to do this (requires O(N)) and a Set is not ordered 1.

We are left with a Map but a Map is a key-value pair.

You can create a map with key as the element and value as the index as an auxiliary data structure to your collection. The downside is you need to update the map when you update your collection (List/Array)

or just use the map and get rid of your collection.


1 indexOf method is on a List and not on Collection.

🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity - Python Wiki
Note that there is a fast-path ... complexity, but it can significantly affect the constant factors: how quickly a typical program finishes. [1] = These operations rely on the "Amortized" part of "Amortized Worst Case". Individual actions may take surprisingly long, depending on the history of the container. [2] = Popping the intermediate element at index k from a list of size n ...
🌐
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 - In this article, we will explore the Big O complexity of common list operations, helping you make informed decisions about algorithm design and performance optimizations. Accessing an element in a Python list by its index is an efficient operation with constant time complexity.