All About NumPy (Lists, Arrays, and Functions)

This post is a comprehensive guide to NumPy, and will go over the following topics:

Lists Vs. Arrays (This section is perfect for anyone who doesn’t have much previous knowledge about Python)

NumPy Functions (This section is better for people who know the basics of Python/have some coding experience)

Lists Vs. Arrays:

NumPy is a Python library that deals with arrays. But what is an array and how is it different from a list?

In Python, lists and arrays are both data structures used to store values. While there are other notable data structures in Python (i.e. tuples, sets, and more), we’ll just be focusing on lists and arrays for now.

Both lists and arrays are ordered (are in a specific order, where items can be referenced/accessed using an index), mutable (able to be changed after it’s creation), and can have duplicates of an item.

Built-in data structures (can be used in Python without imported anything else) (don’t need to be declared)Must be imported from the NumPy package or the array module (need to be declared)
Less compact (take up more memory space)More compact (take up less memory space)
Cannot directly handle mathematical operations (e.g. trying to multiply each element of a list with only one line of code would result in an error)Can easily handle mathematical operations (e.g. each element of an array can be multiplied by 2 with just one line of code!)
Stored in different places in memory (slower to access)Stored in one continuous place in memory (faster to access)

List Code:

Array Code:

exampleList = [1, 2, 3]

import numpy as np
exampleArray = np.array([1, 2, 3])

NumPy Functions:

Congrats! Now that we’ve gone over the differences between lists and arrays, we can jump right into the functions that make NumPy unique. Specifically, there are a lot of functions dedicated to the manipulation of arrays.

Let’s go through some of the basic ones:

Create a 1D array: a = np.array([1, 2, 3])
Create a 2D array: b = np.array([[1, 2, 3], [4, 5, 6]]) or c = np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
Note: For the following functions, I will be referring back to the arrays a, b, and c in order to give examples.

Get the dimension of an array: a.ndim (Output: 1)
                               b.ndim (Output: 2)

Get the shape of an array: a.shape (Output: (3,))
                           b.shape (Output: (2,3))

Get the type of an array: a.dtype (Output: dtype('int32'))
Note: If you want to change the type/size that an array takes up, you can specify that when making the array. Ex: a = np.array([1,2,3]; dtype='int16')

Get the size of an element in the array (in bytes): a.itemsize (Output: 4)
Note: Floats generally take up more space than integers.
Get the size of the array (the number of elements): a.size (Output: 3)
Get the total size of the array: a.nbytes (Ouput: 12)
Note: The total size is equal to the number of elements (a.size) multiplied by the item size (a.itemsize).

Get a specific element [row, column]: c[1,5] (Output: 13)
                                      c[1,-2] (Output: 13)
Get a specific row: c[0,:] (Output: array([1,2,3,4,5,6,7]))
Get a specific column: c[:,2] (Ouput: array([3, 10]))
Get a specific sequence [startindex:endindex:stepsize]: c[0,1:6:2] (Output: array([2,4,6]))
Note: By using an =, these commands can also be used to change the values of the selected sections of the array.

I hope this helps! Please note that a lot of this comes from my actual notes taken while watching this video. Big thanks to (and Keith Galli) for their amazing content!

Leave a Reply

Your email address will not be published. Required fields are marked *