Creating Fixed Length Iterables in PythonΒΆ

If you want to create an iterable of a fixed length in python, use collections.deque with the maxlen parameter.

 1import collections
 2
 3fixed_list = collections.deque(5*[None], 10)
 4
 5# this is now a fixed list of items 10.
 6# by _appending_ more items, you'd be dropping items
 7# from the beginning.
 8
 9fixed_list.append(1)
10print(fixed_list)

Note that this merely calls appendleft.

Comments

comments powered by Disqus