What is the time complexity of popping elements from list in Python?

Question:

I wonder what is the time complexity of pop method of list objects in Python (in CPython particulary). Also does the value of N for list.pop(N) affects the complexity?

Asked By: Haldun

||

Answers:

Pop() for the last element ought to be O(1) since you only need to return the element referred to by the last element in the array and update the index of the last element. I would expect pop() for an arbitrary element to be O(N) and require on average N/2 operations since you would need to move any elements beyond the element you are removing one position up in the array of pointers.

Answered By: tvanfosson

Yes, it is O(1) to pop the last element of a Python list, and O(N) to pop an arbitrary element (since the whole rest of the list has to be shifted).

Here’s a great article on how Python lists are stored and manipulated: http://effbot.org/zone/python-list.htm

Answered By: Dan Lenski

The short answer is look here: https://wiki.python.org/moin/TimeComplexity

With no arguments to pop its O(1)

With an argument to pop:

  • Average time Complexity O(k) (k represents the number passed in as
    an argument for pop
  • Amortized worst case time complexity O(k)
  • Worst case time complexity O(n)

Average time complexity:

  • Any time you put in a value the time complexity of that operation is
    O(n – k).

  • For example, if you have a list of 9 items than removing from the end of the list is 9 operations and removing from the beginning of
    the list is 1 operations (deleting the 0th index and moving all the
    other elements to their current index – 1)

  • Since n – k for the middle element of a list is k operations the average can be shortened to O(k).

  • Another way to think about this is that imagine that each index was removed from your 9 item list once. That would be a total of 45 operations. (9+8+7+6+5+4+3+2+1 = 45)

  • 45 is equal to O(nk) and since the pop operation occurred O(n) times you divide nk by n to get O(k)

Amortized Worst Case Time Complexity

  • Imagine you have a list of 9 items again. Imagine you’re removing every item of the list and the worst case occurs and you remove the first item of the list each time.

  • Since the list shrinks by 1 each time the number of total operations decreases each time from 9 through 1.

  • 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45. 45 equals O(nk). Since you made 9 operations and 9 is O(n) to calculate the amortized worst case scenario you do O(nk) / O(n) which equals O(k)

  • Stating it’s O(n) for the average and amortized worst case time complexity is also sort of correct. Notice that O(k) is approximately O(1/2n) and dropping the constant equals O(n)

Worst Case Time Complexity

  • Unlike with amortized worst case time complexity you don’t factor in the state of the data structure and just think about the worst case for any individual operation.
  • In that instance, the worst case is you have to remove the 1st item from the list which is O(n) time.

Here’s what I wrote to think this through in case it helps:

Answered By: Gwater17

It should be O(1) with L.pop(-1), and O(n) with L.pop(0)

see following example:

from timeit import timeit
if __name__ == "__main__":
    L = range(100000)
    print timeit("L.pop(0)",  setup="from __main__ import L", number=10000)
    L = range(100000)
    print timeit("L.pop(-1)", setup="from __main__ import L", number=10000)

>>> 0.291752411828
>>> 0.00161794329896
Answered By: Pythoner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.