Aryan PrajapatKnowledge Contributor
How to create 1D, 2D and 3D arrays in python?
How to create 1D, 2D and 3D arrays in python?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
Below code helps you create 1D array:
import numpy as np
# creating the list
list = [100, 200, 300, 400]
# creating 1-d array
n = np.array(list)
print(n)
Output: [100 200 300 400] Below code helps you create a 2D array:
import numpy as np
# Create a 2-dimensional array with 3 rows and 4 columns
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# Print the array
print(arr)
Output: [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] Below code helps you create a 3D array:
import numpy as np
# Create a 3D array with shape (2, 3, 4)
nested_list = [[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]],
[[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]]
arr = np.array(nested_list)
print(arr)
Output: [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]]