1

I came from C world where the size of buffers can be controlled in advance. For instance, declaring a buffer of doubles that holds 10 elements, we do:

double *buffer = calloc(10, sizeof(double));

In Python, it is quite the contrary in the sense that we don't need to worry about the details and the size of the buffer by saying buffer=[].

My question is: If I want to restrict a python list to hold an arbitrary number of values, say 10 doubles, how can we do that with default python lists or maybe numpy?

jpp
  • 159,742
  • 34
  • 281
  • 339
steve
  • 153
  • 1
  • 2
  • 9
  • 1
    check out https://stackoverflow.com/questions/311775/python-create-a-list-with-initial-capacity – spooky Apr 18 '18 at 20:52
  • I don't think this is a precise duplicate as user is open to `numpy`. – jpp Apr 18 '18 at 21:00
  • I fail to see how this is a duplicate of the linked question. Initializing to a length and actually restricting the length are not the same thing. – Paul Panzer Apr 18 '18 at 21:28
  • @jpp I added another to the dupe list with the same C foreword even. – miradulo Apr 18 '18 at 21:28
  • @PaulPanzer Did you see the second duplicate in the list there? – miradulo Apr 18 '18 at 21:29
  • Hmm, someone reopened this one. I'll turn my answer into a community wiki if/when it's dup'd correctly. – jpp Apr 18 '18 at 21:31
  • @miradulo nope, sorry, must have missed that, I see only one linked item. Could you be kind enough to repost it for me? – Paul Panzer Apr 18 '18 at 21:32
  • @PaulPanzer No worries. I marked it with [this one](https://stackoverflow.com/questions/6142689/initialising-an-array-of-fixed-size-in-python), though for NumPy [this one](https://stackoverflow.com/questions/4535374/initialize-a-numpy-array) could be added too. I fail to see what is unique about this question. – miradulo Apr 18 '18 at 21:35
  • @miradulo thanks. I'm still not convinced, I don't see any of the answers over there enforcing the list is not resized. And arrays with their slower `__getitem__` are not necessarily a suitable replacement. – Paul Panzer Apr 18 '18 at 21:48
  • @PaulPanzer I suppose we just differ in what we believe the OP is intending to ask. They closed with the first duplicate themselves - and a dynamically allocated array in C does not prevent resizing either. – miradulo Apr 18 '18 at 21:50
  • @miradulo ok, let's leave it at that. If you want to start another close cycle I won't reopen. – Paul Panzer Apr 18 '18 at 21:52
  • @PaulPanzer Sure thing - I already VTCed earlier so I cannot. Here's another: [How to create a fix size list in python?](https://stackoverflow.com/questions/10617045/how-to-create-a-fix-size-list-in-python), with the answers I'd expect. – miradulo Apr 18 '18 at 21:54

2 Answers2

2

To ensure an array is of a fixed size and of a certain type, you should use numpy. This is true even with non-numeric data.

Some examples are below.

import numpy as np

arr = np.zeros(10, dtype=float)
# array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]), dtype = float64

arr = np.zeros(10, dtype=int)
# array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), dtype = int32

arr = np.empty(10, dtype='<U8')
# array(['', '', '', '', '', '', '', '', '', ''],
#       dtype='<U8')
jpp
  • 159,742
  • 34
  • 281
  • 339
1

You can use deque from collections that has a maxlen property.

from collections import deque
d = deque(maxlen=2)

You can then append, pop, iterate or do whatever you want with your restricted list.

Tyler K
  • 328
  • 2
  • 7