Using collections

https://docs.python.org/3/library/collections.html

INTRODUCTION

This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, 'dict', 'list', 'set', and 'tuple'.

DEQUE


import collections

# collections.deque([iterable[, maxlen]])
# Methods: copy(), clear(), count(), index(), insert()
# remove(), reverse(), rotate(n=1),
# extend(iterable), extendleft(iterable),
# append(), appendleft(),
# pop(), popleft().

d = collections.deque('ghi')   # make a new deque with three items
print(d)   # deque(['g', 'h', 'i'])
d.append('j')           # add a new entry to the right side
d.appendleft('f')       # add a new entry to the left side
print(d)   # deque(['f', 'g', 'h', 'i', 'j'])
item1 = d.pop()   # 'j', return and remove the rightmost item
item2 = d.popleft()   # 'f', return and remove the leftmost item
list(d)   # ['g', 'h', 'i'], list the contents of the deque
assert d[0] == 'g' and d[-1] == 'i'   # fast only for the ends
'h' in d   # True
for item in d:   # iteration
    print(item)