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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.