How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (2025)

In this NumPy blog, I will explain how to create a 2D NumPy array in Python using various functions with some illustrative examples. I will also explain how to check if the array is of 2nd dimension or not, and what is its shape and size.

To create a 2D NumPy array in Python, you can utilize various methods provided by the NumPy library. Use np.array with a list of lists for custom values, np.zeros or np.ones for arrays of zeros or ones respectively, np.full to fill with a specific value, np.arange combined with np.reshape for sequential values in a 2D format, and np.random for arrays with random numbers.

Table of Contents

Methods to create a 2D NumPy array in Python

There are six different methods to create a 2D NumPy array in Python:

  1. Using np.array
  2. Using np.zeros
  3. Using np.ones
  4. Using np.full
  5. Using np.arange with np.reshape
  6. Using np.random

Let’s see them one by one using some illustrative examples:

Before this, we will see the process we need to follow to confirm that whatever we have created is a 2D NumPy array in Python.

import numpy as npImports the NumPy library and renames it as ‘np’.
print(type(array_name))Outputs the data type of the variable ‘array_name’ (e.g., NumPy array).
print(np.ndim(array_name))Prints the number of dimensions (e.g., 1D, 2D) of ‘array_name’.
print(array_name.shape)Shows the dimensions of the array ‘array_name’ (e.g., (rows, columns)).
print(array_name.size)Displays the total number of elements in the array ‘array_name’.

Method 1: np 2d array in Python with the np.array() function.

The np.array() function creates a 2D array by passing a list of lists, allowing for manual specification of array contents in Python. For instance:

import numpy as npData = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This will create a 2D array in Python with two rows and four columns.

The array is: [[1 2 3 4] [5 6 7 8]]<class 'numpy.ndarray'>28(2, 4)

We can create a 2D NumPy array in Python by manually specifying array contents using np.array.

Method 2: Create a 2d NumPy array using np.zeros() function

The np.zeros() function in NumPy Python generates a 2D array filled entirely with zeros, useful for initializing arrays with a specific shape and size. For example:

import numpy as npData = np.zeros((2, 3))print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This code creates a 2×3 array filled with zeros through Python NumPy.

The array is: [[0. 0. 0.] [0. 0. 0.]]<class 'numpy.ndarray'>26(2, 3)
How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (2)

This way, to create a 2D NumPy array in Python, we can use the np.zeros function.

Method 3: NumPy 2D array initialize using np.ones() function

The np.ones() function creates a 2D array in Python where all elements are ones, handy for initializing arrays of a specific shape with a default value of one.

import numpy as npData = np.ones((2, 3))print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This creates a 2×3 array filled with ones in Python.

The array is: [[1. 1. 1.] [1. 1. 1.]]<class 'numpy.ndarray'>26(2, 3)
How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (3)

We can create a 2D NumPy array in Python using the np.ones function.

Method 4: How to create a 2d NumPy array using np.full() function

The np.full() function forms a 2D array filled with a specified value, providing control over the initial content of the array in Python.

import numpy as npData = np.full((2, 3), 7)print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This creates a 2×3 array where each element is 7 through Python.

The array is: [[7 7 7] [7 7 7]]<class 'numpy.ndarray'>26(2, 3)
How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (4)

This way, we can create a 2D NumPy array in Python using np.full function.

Method 5: NumPy arange 2D array using np.arange with np.reshape

The np.arange with np.reshape function creates a 1D array with a range of numbers and reshapes it into a 2D array in Python NumPy, offering a way to generate sequential data and format it as needed.

import numpy as npData = np.arange(6).reshape(2, 3)print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This code creates a 2×3 array with values from 0 to 5 in Python.

The array is: [[0 1 2] [3 4 5]]<class 'numpy.ndarray'>26(2, 3)
How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (5)

We can create a 2D NumPy array in Python, using the np.arange with np.reshape function.

Method 6: 2D array using numpy.random function

The np.random() function generates a 2D array with random values, ideal for simulations or when random initial data is required.

import numpy as npData = np.random.random((2, 3))print("The array is:\n", Data)print(type(Data))print(np.ndim(Data))print(Data.size)print(Data.shape)

Output: This creates a 2×3 array with random floating-point numbers through Python.

The array is: [[0.29021833 0.2852124 0.78524396] [0.88524634 0.80241381 0.22616022]]<class 'numpy.ndarray'>26(2, 3)
How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (6)

This way we can create a 2D NumPy array in Python using np.random function.

Conclusion

This article explains how to create a 2D NumPy array in Python using six different methods with examples. Whether we need to manually specify array contents using np.array, initialize arrays with zeros using np.zeros, ones using np.ones, or a specific value using np.full, generate sequential data using np.arange with np.reshape, or create arrays with random elements using np.random, NumPy provides a straightforward and efficient solution.

Understanding these methods is essential for anyone working in data science, scientific computing, or any field that requires efficient and effective numerical computation in Python.

You may also like to read:

  • Python NumPy Array
  • Python NumPy zeros
  • Python NumPy arange
  • How to create an empty matrix in Python

How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (7)

Bijay Kumar

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

How To Create A 2D NumPy Array In Python [6 Methods] - Python Guides (2025)

FAQs

How to create a 2 dimensional array in Python NumPy? ›

To create NumPy 2D array use array() function and give one argument of items of lists of the list to it. In machine learning and data science NumPy 2D array known as a matrix. Specially use to store and perform an operation on input values.

How do you create a random 2D array in Python using NumPy? ›

Randomly Constructing 2D Array

In this example This Python code uses the NumPy library to create a 3×4 2D array filled with random values between 0 and 1 using the `numpy. random. rand()` method.

How do you write a 2D array in Python? ›

To create a 2D array in Python, you can use nested lists. EX: array = [[1, 2], [3, 4], [5, 6]] . This involves creating a list within a list, where each inner list represents a row in the 2D array. In this example, we've created a 2D array (or a matrix) with three rows and three columns.

How to plot a 2D NumPy array Python? ›

Scatter a 2D numpy array in matplotlib
  1. Set the figure size and adjust the padding between and around the subplots.
  2. Create random data of 100×3 dimension.
  3. Use the scatter() method to plot 2D numpy array, i.e., data.
  4. To display the figure, use show() method.
Feb 2, 2022

How to create a 2D array? ›

To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.

How do you create an empty 2D array in Python using NumPy? ›

Use empty() Function with 2- D Array

To create a two-dimensional array of empty use the shape of columns and rows as the value to shape parameter. We passed a list of numbers, [5,3] to the shape parameter. This indicates to numpy. empty() that we want to create an empty NumPy array with 5 rows and 3 columns.

How do you create a 2D array in Python on one line? ›

Initializing 2D Array

First, the array arr is initialized using a 2D list comprehension, where each row is created as [0, 0, 0, 0, 0] . The entire array is created as a list of references to the same inner list, resulting in aliasing. Any change made to an element in one row will be reflected in all rows.

How to create 2D array from 2 1D array in Python? ›

  1. You can use the numpy library to convert a 1D array to a 2D array in Python. The syntax is as follows:
  2. 2D_array = numpy.reshape(1D_array, (rows, columns))
  3. Where rows and columns are the number of rows and columns for the 2D array, respectively.
Mar 21, 2023

What are ways of creating 1D, 2D, and 3D arrays in NumPy? ›

a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists will create higher-dimensional arrays. In general, any array object is called an ndarray in NumPy.

How to add two 2D arrays in NumPy? ›

Using NumPy, we can perform concatenation of multiple 2D arrays in various ways and methods.
  1. Method 1: Using concatenate() function.
  2. Method 2: Using stack() functions:
  3. Method 3: Using hstack() function.
  4. Method 4: Using vstack() function.
  5. Method 5: Using dstack() function.
Aug 9, 2021

What is the correct way to create a NumPy array? ›

You first import NumPy and then use the array() function to create an array. The array() function takes a list as an input. The type of my_array is a numpy. ndarray .

How to convert NumPy array to 2D? ›

To convert a 1-D array into a 2-D column vector, an additional dimension must be added, e.g., np.atleast_2d(a).T achieves this, as does a[:, np.newaxis] . For a 2-D array, this is the standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples).

How to make a 2D array from 2 1D arrays NumPy? ›

c = np. dstack((a, b)): The np. dstack() function is used to stack the two arrays 'a' and 'b' depth-wise along the third axis. The resulting array 'c' has the shape (3, 1, 2), where the first depth layer contains the elements of a and the second depth layer contains the elements of 'b'.

How to create 2x3 matrix in NumPy? ›

Explanation: np_array = np. array([[1, 2, 3], [2, 1, 2]], np. int32) creates a 2x3 NumPy array with elements of type int32.

How to take 2D NumPy array as input in Python? ›

Here I have mentioned the basic to take 2d array input:
  1. n_rows= int(input("Number of rows:"))
  2. n_columns = int(input("Number of columns:"))
  3. #Define the matrix.
  4. matrix = [ ]
  5. print("Enter the entries row-wise:")
  6. #for user input.
  7. for i in range(n_rows): # A for loop for row entries.
  8. a =[ ]
May 11, 2020

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5399

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.