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?

Prog1020
  • 4,530
  • 8
  • 31
  • 65

2 Answers2

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.

Daniel Perez
  • 6,335
  • 4
  • 24
  • 28
  • I've seen some stability issues with `bitarray`. There seems to be a segfault affecting the [`itersearch`](http://stackoverflow.com/questions/17981959/create-a-list-like-object-using-a-bitarray) method, at least. I'm not sure whether the module is actively maintained, so there might not be fixes coming. – user2357112 Sep 16 '13 at 22:07
  • 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](https://github.com/ilanschnell/bitarray/issues/12) you are probably talking about but I could not reproduce it. Are you still having the same issue? – Daniel Perez Sep 16 '13 at 22:14
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?

Community
  • 1
  • 1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179