Prog1020
Prog1020

Reputation: 4781

Python array for bool values?

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?

Upvotes: 2

Views: 183

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 113978

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

Upvotes: 0

Daniel Perez
Daniel Perez

Reputation: 6903

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.

Upvotes: 3

Related Questions