0

If I want to make an empty list that is a certain length, how do I create it without having to manually type the brackets. For example: If I want to have a list with a length of 32 but I don't want to have to type [ , , , , , ...] until the 32nd element, how would I do that?

Thank you.

mmason3
  • 1
  • 1
  • 2

2 Answers2

2

You can directly do it as -

empty_list = [None]*32
print(empty_list)

OUTPUT :

[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

The above will create a list of size 32, where each element of the list is initialized to None.

You can also do it as -

empty_list = [None for _ in range(32)]

The above code would also give the same result as before

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • 1
    It should be noted that the first solution shouldn't be used for any initialized value: they will all point to the same object. For example: `a = [[]] * 10` will actually have a list point to the same empty list 10 times. The two ways shown do not always provide the same results, just with `None`. – M Z Jul 27 '20 at 20:37
0

What about:

a = numpy.empty(32, str).tolist()

or:

a = [‘’]*32
S3DEV
  • 8,768
  • 3
  • 31
  • 42