Skip to Main Content
It looks like you're using Internet Explorer 11 or older. This website works best with modern browsers such as the latest versions of Chrome, Firefox, Safari, and Edge. If you continue with this browser, you may see unexpected results.

Python for Basic Data Analysis: NP.2 Create Arrays Using lists

Get started on your learning journey towards data science using Python. Equip yourself with practical skills in Python programming for the purpose of basic data manipulation and analysis.

Creating arrays with lists

Create arrays using lists

Let's begin with the basics of NumPy. There are different ways to create NumPy arrays but the easiest way will be to use something that we are all familiar with, which are lists, and pass it to the array() function.

Syntax

nameofarray = np.array(lists)


Below are some examples. Click on the Run button in each trinket to view the results.


Create a rank 1 (1D) array from a list
 


Create a rank 2 (2D) array from a list
 

Check data type in arrays

To find out what data types are in an array, you can use .dtype

Syntax

print(myArray.dtype)

Editing elements in array

You can edit an element in an array by referring to its index.

Recall that  myArray = [1 2 3 4 5 6 7].  You can change the first element of myArray from 1 to 20 by referring to its index (which is 0).
 

myArray[0] = 20

 

Shape of array

You can also check the shape of an array (i.e. number of rows and columns) using .shape

Exercises

1. Create a rank 2 (2D) array that resembles the matrix below.

[[11 12 13 14]
 [15 16 17 18]]


2. Find out the shape of the above array

Video Guide