Python: list is array or array is list?

Array manipulation

One of the first lesson of Python is that in Python we have: tuple, list and dictionary. While other libraries "array" is the most popular way to arrange a list of objects, integer, string, etc, etc, etc.... in Python we say that is a list, but is that 100% true? For my point of view, someone who came from Java and JavaScript list will be always the same of an Array.

The most popular library in Python for sure is Numpy, and Numpy have a lot of ways to manipulate an array, let see some methods to manipulate an Array.

import numpy as np
  • Attributes: size, shape, and data types of arrays
  • Indexing: Getting and setting the value of individual array elements
  • Slicing: Getting and setting smaller subarrays within a larger array

Size: can have one dimension, two dimension, n dimension.

one_dim_array = np.random.randint(10, size=6)  # One-dimensional array
two_dim_array = np.random.randint(10, size=(3, 4))  # Two-dimensional array

print(one_dim_array) # output: array([3, 2, 0, 4, 1, 5])

Shape: the dimension of the array, how the array is presented.

print(one_dim_array.shape) # output: (6,)
print(two_dim_array.shape) # output: (3, 4)

Type: what the type of data it's inside the array

print(one_dim_array.dtype) # output: dtype('int64')

Indexing: like other languages yourArray[x] -> where x is the number of the index of the element inside the array starting from 0, but the coolest thing that I learned in Python is to access from the end.

one_dim_array[-1] # output: 5

In a multi-dimensional array, items can be accessed using a comma-separated tuple of indices.

Slicing: The slicing syntax follow yourArray[start:stop:step] when you understand, will blow your mind, well, blowed mine.

x = np.arange(10)
print(x) # output: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[:5]  # first five elements, output: array([0, 1, 2, 3, 4])
x[5:]  # elements after index 5, output: array([5, 6, 7, 8, 9])
x[4:7]  # middle sub-array, output: array([4, 5, 6])
x[::2]  # every other element, output: array([0, 2, 4, 6, 8])

The very interesting case is when you want to reverse an array and the simple way is use the slicing with negative step.

x[::-1]  # all elements, reversed, output: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

Slicing a more than one dimension array is more trick, works with multiple slices separated by commas. When we print the random second_dim_array created before:

# output:  array([[0, 2, 5, 7],
                            [8, 8, 8, 3],
                            [4, 4, 4, 5]])
x2[:2, :3]  # two rows, three columns
# output: array([[12,  5,  2],
       [ 7,  6,  8]])

It's a World, and like me you can learn more reading the book: Python Data Science Handbook

pydatasc.png

Numpy Cheat Sheet by DataCamp