Skip to main content

Command Palette

Search for a command to run...

NumPy Array Operations Every Beginner Should Actually Know

Updated
7 min readView as Markdown
NumPy Array Operations Every Beginner Should Actually Know

When I started learning NumPy, arrays themselves weren't really the confusing part. Creating one was easy. Printing it was easy. Even accessing an element was easy.

The confusion started when I had to actually manipulate the array.

I would see functions like reshape(), resize(), insert(), delete(), concatenate() and split() and think, “Okay... but which one am I supposed to use here?”

If you're learning NumPy for the first time, you probably don't need to memorize dozens of functions. You need to understand a few important operations properly and know when to use them. So let's go through seven of them with small examples instead of turning this into a giant list of functions.

Creating an Array with np.array()

Before doing anything with an array, we need to create one. NumPy provides the np.array() function for this. It can convert a Python list or another sequence into a NumPy array.

import numpy as np

marks = np.array([78, 85, 91, 67, 88])

print(marks)

The output will be:

[78 85 91 67 88]

This is usually where your NumPy journey begins. Once your data is converted into an array, you can use NumPy's different numerical and array-manipulation operations on it. If you're coming from Python lists, the syntax might look familiar, but arrays give you much more functionality for numerical computing.

Accessing Data with Indexing

Creating an array is only useful if you can actually access the data inside it. NumPy follows Python's zero-based indexing, so the first element has index 0.

marks = np.array([78, 85, 91, 67, 88])

print(marks[0]) print(marks[2])

This gives:

78 91

You can also use indexing with multidimensional arrays. For example, suppose you have marks stored for two different rows:

marks = np.array([ [78, 85, 91], [67, 88, 72] ])

print(marks[1, 1])

The result is 88 because we're selecting the second row and the second column. Once you start working with matrices, datasets, or image data, this type of indexing becomes something you'll use constantly.

Changing Structure with reshape()

Here's where arrays start becoming much more interesting. Suppose you have six numbers stored in a single array, but you want to arrange them into two rows and three columns.

You can use reshape() instead of creating a completely new array.

numbers = np.array([1, 2, 3, 4, 5, 6])

matrix = numbers.reshape(2, 3)

print(matrix)

The result is:

[[1 2 3] [4 5 6]]

Notice that none of the values changed. We simply changed how they are arranged. That's the main idea behind reshape().

There is one important rule here: the total number of elements must remain compatible with the new shape. Since our array contains six elements, 2 × 3 works, but 2 × 4 doesn't because that would require eight elements.

So whenever you see reshape(), think same data, different structure.

Changing the Size with resize()

This is one of those places where NumPy's naming can confuse beginners. reshape() and resize() sound almost identical, but they are not doing the same job.

While reshape() changes the arrangement of existing elements, resize() can change the size of the resulting array.

numbers = np.array([1, 2, 3, 4])

result = np.resize(numbers, (2, 3))

print(result)

The output is:

[[1 2 3] [4 1 2]]

Our original array had four elements, but the requested shape needs six. NumPy fills the extra positions by repeating values from the original array.

This is why remembering the difference between these two functions is important. If you only want to rearrange your existing data, reshape() is what you're looking for. If the size itself needs to change, resize() can be useful.

Adding an Element with np.insert()

Now imagine you already have an array and suddenly need to add a value somewhere in the middle.

For example:

numbers = np.array([10, 20, 40, 50])

updated = np.insert(numbers, 2, 30)

print(updated)

The output is:

[10 20 30 40 50]

Here, 2 is the index where the new value is inserted and 30 is the value we're adding.

One small thing worth remembering is that np.insert() returns a new array. It doesn't simply behave like modifying a Python list in place. That's why we're storing the result in updated.

This distinction becomes more important as your programs get larger because you need to know whether an operation changes the original array or gives you a new one.

Removing an Element with np.delete()

Of course, manipulating data isn't always about adding something. Sometimes you need to remove something that shouldn't be there.

That's where np.delete() comes in.

numbers = np.array([10, 20, 30, 40, 50])

updated = np.delete(numbers, 2)

print(updated)

The output is:

[10 20 40 50]

The element at index 2, which is 30, has been removed from the resulting array.

Just like np.insert(), np.delete() returns a new array. This is particularly useful when you're cleaning or preparing numerical data before using it for further processing.

Combining and Dividing Arrays

Sometimes your data isn't stored in one array. You might have two separate arrays that you want to combine into one. NumPy's concatenate() function can help with that.

first = np.array([1, 2, 3]) second = np.array([4, 5, 6])

combined = np.concatenate((first, second))

print(combined)

The result is:

[1 2 3 4 5 6]

Now imagine the opposite situation. Instead of combining arrays, you have one large array and want to divide it into smaller parts. That's where np.split() becomes useful.

numbers = np.array([1, 2, 3, 4, 5, 6])

parts = np.split(numbers, 3)

print(parts)

You'll get three smaller arrays:

[array([1, 2]), array([3, 4]), array([5, 6])]

These two functions are useful when you're reorganizing data. One brings separate arrays together, while the other breaks an array into smaller sections.

The Real Trick Isn't Memorizing the Functions

If you're learning NumPy, it's tempting to make a giant cheat sheet and try to memorize every function you find.

I wouldn't recommend that.

What helped me more was connecting each function to the problem it solves. If the data is correct but its arrangement needs to change, think reshape(). If you need to change the size, think resize(). If you need to add or remove something, insert() and delete() make sense. And when you're working with multiple arrays, concatenate() and split() give you a way to bring data together or separate it.

Once you start thinking this way, you don't have to remember the syntax perfectly every single time. You just need to know what you're trying to accomplish, and the documentation can take care of the exact syntax when you forget it.

A Small Challenge Before You Move On

Here's something I'd recommend trying instead of simply closing this article after reading it.

Create a NumPy array containing the numbers from 1 to 12. Try turning it into a 3 × 4 structure, resize it into a different shape, insert a new value, delete one value, and then split the array into smaller parts.

Don't worry if you get an error.

Actually, getting an error is useful here. Try to understand why NumPy rejected your operation. For example, if reshape() doesn't work, check whether the number of elements actually matches the shape you're asking for.

That kind of experimentation will make these operations stick much better than memorizing definitions.

Final Thoughts

These seven operations might look basic, but they're going to appear again once you start working with real numerical data, matrices, machine learning datasets, and eventually AI projects.

The goal isn't to know every NumPy function by heart. The goal is to become comfortable enough with arrays that manipulating them doesn't feel like a completely separate problem every time.

And if you're still building your foundation, I've also written a beginner-friendly NumPy Basics Guide where I explain what NumPy is, why it is used, and the concepts you should understand before going deeper.

Start small. Write the code yourself. Change the values. Break the array. Fix it.

That's where NumPy actually starts making sense.