Junior — Middle
What is the time complexity of accessing an element by its position in a list?
sobes.tech AI
Answer from AI
In Python, lists are implemented as dynamic arrays. Accessing an element by its position (index) has a time complexity of O(1), which means constant time. This indicates that regardless of the size of the list, retrieving an element by index is fast and efficient.
Example:
lst = [10, 20, 30, 40]
print(lst[2]) # Outputs 30