130k views
2 votes
What is the runtime complexity of the list's built-in .append() method?

1 Answer

3 votes

Final answer:

The runtime complexity of the list's built-in .append() method is O(1), or constant time complexity, except for rare cases where it may momentarily be O(n) due to memory reallocation.

Step-by-step explanation:

The runtime complexity of the list's built-in .append() method is O(1), also known as constant time complexity. When you use the .append() method to add an item to the end of a list in Python, it typically takes the same amount of time regardless of the size of the list. However, in certain situations, when the list needs to grow beyond its current allocated space, Python may need to allocate more space which can momentarily cause the method to run in O(n) time complexity, where n is the number of elements in the list. This is rare and amortized over many .append() operations, so the average complexity remains constant.

User Espvar
by
8.3k points