If you are working with bytes you could use the builtin bytearray
. If you are working with other integral types look at the builtin array
.
Specifically understand that a list
is not an array
.
If, for example, you are trying to create a buffer for reading file contents into you could use bytearray as follows (there are better ways to do this but the example is valid):
with open(FILENAME, 'rb') as f:
data = bytearray(os.path.getsize(FILENAME))
f.readinto(data)
In this snippet the bytearray
memory is preallocated with the fixed length of FILENAME
s size in bytes. This preallocation allows the use of the buffer protocol to more efficiently read the file into a mutable buffer without an array copy. There are yet better ways to do this but I believe this provides one answer to your question.