1

Let's say I have an array that is 10 x 100,000. What is the simplest and/or fastest way to initialize this? For example, something like:

[None,] * cols # now how to do it by # rows?
1
  • Something like numpy.full ((10,100000), None) Commented May 9, 2020 at 5:33

2 Answers 2

3

If you want to do this with vanilla Python lists, I would use a list comprehension:

big_array = [[None]*100000 for j in range(10)]

However, if you are going to be working with large arrays a lot, I would consider using using numpy:

import numpy as np
another_big_array = np.empty((10, 100000))

With numpy, be sure you get all 4 parentheses. np.empty() takes a single argument for the shape of the array, so for multidimensional arrays you need a tuple of integers, not multiple arguments.

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

The inner comprehension can be replaced with [None]*100000 for a speed boost. (The outer comprehension cannot be replaced this way, or you'll have aliasing problems.)
0

Try using the numpy.full method.

Example of a 2x2 array

 import numpy as np

 none_array = np.full((2, 2), None)
 print(none_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.