I know array.array allows to have int/float array. How to have bool array? Memory efficient. so that 1 value is stored as 1 bit. does array support it?
2 Answers
As far as I know there is nothing to store booleans this efficiently in native Python, but you can checkout the bitarray library which is I think what you are looking for.
2 Comments
user2357112
I've seen some stability issues with
bitarray. There seems to be a segfault affecting the itersearch method, at least. I'm not sure whether the module is actively maintained, so there might not be fixes coming.Daniel Perez
I did not have any problem while using it with Python 3 (for what I used it at least). I checked out the Github issue you are probably talking about but I could not reproduce it. Are you still having the same issue?
>>> sys.getsizeof(int)
436
>>> sys.getsizeof(bool)
436
>>> sys.getsizeof(bool())
12
>>> sys.getsizeof(int())
12
>>>
basically even if you could you would not get any space saving ...
this may also be of interest
3 Comments
Prog1020
This doesn't help me with "array" sctruct.
Joran Beasley
it was more to demonstrate that a bool is more than 1 bit in python
Prog1020
Ye, i know, so I need "not native" struct.
array.arrayisn'tnumpy.