Usually, it's best to pay the expense of using a byte per value rather than a bit, and you can use bytearray
(a built-in since 2.6) for this purpose:
a = bytearray(100) # 100 values all initialized to 0/False
# or initially true:
b = bytearray(b'\x01' * 100) # 100 values all initialized to 1/True
# While you'll get 0 and 1 back, True and False can be assigned to it
a[1] = True
b[1] = False
This is usually be the best option, as it's more efficient to use byte addressing in most cases, unless it would cause data to spill from RAM to a swap file.
If you really need the space for a lot of flags, you'd need a third party package that optimizes to get one bit per value, e.g. bitarray
(C extension for maximum speed, but still slower than bytearray
for many purposes) or bitvector
or bitstring
(Pure Python, to minimize compilation complications, and sometimes provide additional features more easily, but reliably slower than bytearray
when not memory constrained).