Python list is more than a list


In [1]:
L = list(range(10))

In [2]:
L


Out[2]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]:
type(L)


Out[3]:
list

In [4]:
type(L[0])


Out[4]:
int

In [5]:
L2 = [str(c) for c in L]
type(L2[0])


Out[5]:
str

In [6]:
all(type(e) == int for e in L)


Out[6]:
True

List can be heterogeneous list


In [7]:
L3 = [True, '2', 3.0, 4]
[type(item) for item in L3]


Out[7]:
[bool, str, float, int]

In [8]:
tuple(L3)


Out[8]:
(True, '2', 3.0, 4)

Creating Arrays from lists


In [9]:
import numpy as np
np.array([1, 4, 2, 5, 3])


Out[9]:
array([1, 4, 2, 5, 3])

In [10]:
np.array([3.14, 4, 2, 3])


Out[10]:
array([3.14, 4.  , 2.  , 3.  ])

Creating Arrays from scratch

Especially for larger arrays, it is more efficient to create arrays from scratch using routines built into Numpy. Here are some examples:


In [11]:
# create a random integer array
np.random.randint(0, 10, (3, 3))


Out[11]:
array([[0, 9, 9],
       [1, 1, 2],
       [1, 9, 7]])

In [12]:
# create a 3x3 array of uniform random values
np.random.random((3, 3))


Out[12]:
array([[0.35426557, 0.04430429, 0.41836319],
       [0.31968574, 0.37538208, 0.33031082],
       [0.64993674, 0.74265398, 0.20838379]])

In [13]:
# create 3x3 array of normally distributed data
np.random.normal(0, 1, (3,3))


Out[13]:
array([[-1.75655372,  0.47198179, -0.92343641],
       [ 1.63128262,  1.08827514, -2.33014078],
       [ 0.51843925,  0.41162106, -1.09581601]])

In [ ]: