Understanding Arrays in Python: Types and Creation Methods
This article explains the concept of arrays, outlines common array types such as Python lists, NumPy arrays, and sparse arrays, and provides eleven practical code examples for creating arrays using plain Python, NumPy, pandas, and TensorFlow.
When referring to arrays, we usually mean a data structure used in programming to store and manipulate multiple elements of the same type, with implementations varying across languages and libraries.
Below are several common array types and their characteristics:
Python List (List): A dynamic array that can hold elements of different types, created with square brackets [] and whose length can change.
NumPy Array: A homogeneous, fixed‑size multi‑dimensional array provided by the NumPy library, optimized for vectorized numerical computations.
Array Libraries: Libraries such as pandas, TensorFlow, and PyTorch offer higher‑level array structures for data analysis, machine learning, and deep learning.
Static vs. Dynamic Arrays: Static arrays have a fixed size defined at creation, while dynamic arrays can grow as needed.
Multidimensional Arrays: Beyond one‑dimensional vectors, arrays can be two‑dimensional matrices or higher‑dimensional tensors for complex data like images or audio.
Sparse Arrays: Specialized arrays that store only non‑zero or non‑missing elements to save space and improve efficiency.
Creating Arrays:
1. Using a Python list: my_array = [1, 2, 3, 4, 5] 2. Using NumPy:
import numpy as np
my_array = np.array([1, 2, 3, 4, 5])3. Using a list comprehension: my_array = [i for i in range(1, 6)] 4. Using NumPy helper functions:
import numpy as np
my_zeros_array = np.zeros(5) # create all‑zero array
my_ones_array = np.ones(5) # create all‑one array
my_random_array = np.random.rand(5) # create random array5. Using the range function: my_array = list(range(1, 6)) 6. Using NumPy arange:
import numpy as np
my_array = np.arange(1, 6)7. Using NumPy linspace:
import numpy as np
my_array = np.linspace(1, 10, 5)8. Using zeros_like and ones_like:
import numpy as np
existing_array = np.array([1, 2, 3, 4, 5])
my_zeros_array = np.zeros_like(existing_array)
my_ones_array = np.ones_like(existing_array)9. Using reshape:
import numpy as np
existing_array = np.array([1, 2, 3, 4, 5, 6])
my_array = existing_array.reshape((2, 3))10. Using fromiter:
import numpy as np
my_iterable = [1, 2, 3, 4, 5]
my_array = np.fromiter(my_iterable, dtype=int)11. Using pandas:
import pandas as pd
my_series = pd.Series([1, 2, 3, 4, 5])
my_array = my_series.to_numpy()12. Using TensorFlow:
import tensorflow as tf
my_tensor = tf.constant([1, 2, 3, 4, 5])
my_array = my_tensor.numpy()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
