66

All the solutions I found were for lists.

Thanks.

Yan
  • 3,249
  • 4
  • 20
  • 12

2 Answers2

96

This will give you 100 zero bytes:

bytearray(100)

Or filling the array with non zero values:

bytearray([1] * 100)
antonio
  • 18,044
  • 4
  • 45
  • 61
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
22

For bytes, one may also use the literal form b'\0' * 100.

# Python 3.6.4 (64-bit), Windows 10
from timeit import timeit
print(timeit(r'b"\0" * 100'))  # 0.04987576772443264
print(timeit('bytes(100)'))  # 0.1353608166305015

Update1: With constant folding in Python 3.7, the literal from is now almost 20 times faster.

Update2: Apparently constant folding has a limit:

>>> from dis import dis
>>> dis(r'b"\0" * 4096')
  1           0 LOAD_CONST               0 (b'\x00\x00\x00...')
              2 RETURN_VALUE
>>> dis(r'b"\0" * 4097')
  1           0 LOAD_CONST               0 (b'\x00')
              2 LOAD_CONST               1 (4097)
              4 BINARY_MULTIPLY
              6 RETURN_VALUE
AXO
  • 8,198
  • 6
  • 62
  • 63
  • Am I misunderstanding how timeit works? When I type the following: >>> r'b"\0" * 100" python returns: 'b"\\0" * 100' So, is this example really timing the creation of 100 elements? If I type in print(timeit("b'0x00' * 100")) I get a significantly longer time. – e-holder Oct 25 '18 at 16:09
  • @e-holder `timeit` passes the string statement to `compile` and then `exec`. Why would wrapping `timeit` in a `print` function have a significant effect on the timer output? It's just printing a float *after* the timing is finished. I cannot reproduce. – AXO Oct 25 '18 at 17:40
  • My confusion is whether the leading 'r' is causing the expression to be merely the definition of a string versus an expression to generate a 100 element array. If I enter the expression >>> b = r'b"\0" * 100' >>> b All I get is a string as the value of b. But if I enter the expression >>> b = b"\0" * 100 >>> b I get a 100 element array as the value of b. Do you see why I am confused? – e-holder Oct 25 '18 at 18:11
  • 1
    Okay. I am wrong. print(timeit(r'b"\0" * 100000') takes significantly longer, so it isn't just building a string. – e-holder Oct 25 '18 at 18:46
  • @AXO Could you please clarify whether this performance speedup also applies if the size is not actually a constant? – Tomáš Zato Nov 04 '22 at 17:22
  • 1
    @TomášZato Testing on Python 3.11, `b'\0' * int_var` is almost 1.76 times faster than `bytearray(int_var)` where `int_var = 100`, but of course this is not as dramatic as the constant folding speedup and also slower than using an integer literal. – AXO Nov 04 '22 at 21:38
  • @AXO This bring up the question of which type is *dynamically* allocated vs. a *pre-allocated* array being filled? Any insight? – not2qubit Nov 24 '22 at 21:25
  • @not2qubit Interesting, but sorry, I don't know. My guess is that if the final size is somehow known to the interpreter before it starts to create the object, then it will try to use it to preallocate (like how it does for [`[None] * integer`](https://stackoverflow.com/questions/22225666/pre-allocating-a-list-of-none)). – AXO Nov 24 '22 at 22:33