3 Comments

A couple of extra points I couldn't fit in in the main article without making it unnecessarily longer.

I've used `np.array()` to create a `ndarray` object in the article. You may also see a similar function called `np.asarray()`. The first one of these, `np.array()` will always create a new `ndarray`, whereas `np.asarray()` will only create a new array if the argument isn't already a `ndarray` object.

Secondly, you recall when I introduced the `array` data type from the `array` module? The `array` object took up significantly less memory than its corresponding list. The list was 8448728 bytes while the `array` was 4000080 bytes.

How about the NumPy `ndarray`? If you convert the list you used when comparing lists and `array.array` objects into a NumPy array:

`numbers_np = np.array(numbers_list)`

and then find its size in the same way, using `sys.getsizeof()`, you'll find that the `ndarray` you created is 8000112 bytes, not too far off from the size of the list.

However, if you check the `dtype` of this `ndarray`, you'll see it's:

dtype('int64')

But if you don't need 64-bit integers, you can opt for integer types that that up less memory, such as int32 or even int16 in this case:

```

>>> numbers_np = np.array(numbers_list, dtype="int32")

>>> sys.getsizeof(numbers_np)

4000112

>>> numbers_np = np.array(numbers_list, dtype="int16")

>>> sys.getsizeof(numbers_np)

2000112

```

Expand full comment

"You can use the multiline formatting option to create this array if it's easier to visualise—in Python's REPL/Console environment"

I'd been wondering what all those ellipses on your code snippets were about!

Expand full comment

Ah yes. If you're in Python's default REPL/Console, it will show three chevrons as a prompt (>>>) but ellipsis when there's a block of code, such as a `for` loop, say, or a multiline expression

Expand full comment