Junior — Middle
What is the efficiency level of adding elements to a list?
sobes.tech AI
Answer from AI
In Python, the operation of adding an element to a list using the append() method typically has an amortized time complexity of O(1). This means that on average, adding an element takes constant time.
However, when the internal array of the list overflows, a memory reallocation and copying of elements occur, which takes more time. But such operations are rare, so the average efficiency remains high.
Example:
my_list = []
my_list.append(10) # Fast addition of an element