You really want to be slicing here:
>>> a = [(1,2),(3,4),(5,6),(7,8)]
>>> b = [(1,5),(7,7),(2,9),(0,3)]
>>> a[1:-1]
[(3, 4), (5, 6)]
>>> b[1:-1]
[(7, 7), (2, 9)]
If you need to pop items from both ends regularly, consider using a collections.deque
instead:
>>> from collections import deque
>>> a = [(1,2),(3,4),(5,6),(7,8)]
>>> b = [(1,5),(7,7),(2,9),(0,3)]
>>> a, b = deque(a), deque(b)
>>> a.popleft()
(1, 2)
>>> a.pop()
(7, 8)
>>> a
deque([(3, 4), (5, 6)])
>>> b.popleft()
(1, 5)
>>> b.pop()
(0, 3)
>>> b
deque([(7, 7), (2, 9)])
list.pop(0)
is an O(N) operation, whereas collections.deque.popleft()
is an O(1) operation.
It even recommends this in https://wiki.python.org/moin/TimeComplexity:
Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.