Skip to main content
Practice

Creating 1D and 2D Arrays

In NumPy, arrays are the primary structure for storing and processing data.

You can create them using the np.array() function.

This lesson focuses on two common types: 1D arrays and 2D arrays.


About Notebooks

CodeFriends uses Notebooks to run Python code interactively.

They allow you to write and execute small blocks of code called cells and instantly view the output.

You’ll use them throughout this course to experiment with NumPy and practice creating arrays.


1D Array

A 1D array represents a single sequence of numbers.

You can create one from a standard Python list.

Creating a 1D Array
import numpy as np

arr1d = np.array([10, 20, 30])

print(arr1d) # [10 20 30]
print(arr1d.shape) # (3,)
print(arr1d.ndim) # 1
print(arr1d.size) # 3
  • .shape: number of elements (3,)
  • .ndim: 1 dimension
  • .size: total number of items

2D Array

A 2D array is structured like a table with rows and columns.

You create it by passing a list of lists to np.array().

Creating a 2D Array
arr2d = np.array([
[1, 2, 3],
[4, 5, 6]
])

print(arr2d)
# [[1 2 3]
# [4 5 6]]

print(arr2d.shape) # (2, 3)
print(arr2d.ndim) # 2
print(arr2d.size) # 6

This array has:

  • 2 rows and 3 columns
  • Shape (2, 3)
  • 6 total elements

Summary

Use np.array() to create arrays from lists:

  • A single list: 1D array
  • A list of lists: 2D array

You can always inspect an array with:

  • .shape: dimensions (rows, columns)
  • .ndim: number of dimensions
  • .size: total elements

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.