Python Datatype for a fixed-length FIFO
x = collections.deque(5*[0], 5)
See the docs for more about collections.deque
; the method you call push
is actually called appendleft
in that type.
The second parameter (maxlen
, giving the maximum lengths) was added in Python 2.6; if you're using older versions of Python, it won't be available.
you can also use list
a = [0,0,0,0,0]
a.pop(0)
a.append(1)
print a
result [0,0,0,0,1]
or for left side in right out, otherwise
a.pop(5)
a.insert(0,1)
print a
result [1,0,0,0,0]