From lists to unidimensional arrays

One of the most common situations you will encounter when working with data is transforming a list into an array.

When operating such a transformation, it is important to consider the objects the lists contain because this will determine the dimensionality and the dtype of the resulting array.

Let's start with this first example of a list containing just integers:

In: import numpy as npIn: # Transform a list into a uni-dimensional array    list_of_ints = [1,2,3]    Array_1 = np.array(list_of_ints)In: Array_1Out: array([1, 2, 3]) 

Remember that you can access a one-dimensional array as you would with a standard Python list (the indexing starts from zero):

In: Array_1[1] # let's output the second valueOut: 2  

Get Python Data Science Essentials - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.