2

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
  • 2
    probably not ... and if that concerns you , python may not be the ideal language for this project Commented Sep 16, 2013 at 21:58
  • 2
    array.array isn't numpy. Commented Sep 16, 2013 at 21:59

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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?
0
>>> 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

"sys.getsizeof(int)" returns an unreasonably large value?

3 Comments

This doesn't help me with "array" sctruct.
it was more to demonstrate that a bool is more than 1 bit in python
Ye, i know, so I need "not native" struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.