How do you create a dimensional array in python?

Contents

  • Introduction
  • Create 1D Numpy Array using array() function
  • Create 1D Numpy Array using arange() function
  • Create 1D Numpy Array using linspace() function
  • Summary

One dimensional array contains elements only in one dimension. In other words, the shape of the numpy array should contain only one value in the tuple.

To create a one dimensional array in Numpy, you can use either of the array(), arange() or linspace() numpy functions.

Create 1D Numpy Array using array() function

Numpy array() functions takes a list of elements as argument and returns a one-dimensional array.

In this example, we will import numpy library and use array() function to crate a one dimensional numpy array.

Python Program

import numpy as np

#create numpy array
a = np.array([5, 8, 12])
print(a)

Run

Output

[ 5, 8, 12]

Create 1D Numpy Array using arange() function

Numpy arange() function takes start, end of a range and the interval as arguments and returns a one-dimensional array.

[start, start+interval, start+2*interval, ... ]

Run

In this example, we will import numpy library and use arange() function to crate a one dimensional numpy array.

Python Program

import numpy as np

#create numpy array
a = np.arange(5, 14, 2)
print(a)

Run

Output

[ 5, 7, 9, 11, 13]

Array starts with 5 and continues till 14 in the interval of 2.

Create 1D Numpy Array using linspace() function

Numpy linspace() functions takes start, end and the number of elements to be created as arguments and creates a one-dimensional array.

In this example, we will import numpy library and use linspace() function to crate a one dimensional numpy array.

Python Program

import numpy as np

#create numpy array
a = np.linspace(5, 25, 4)
print(a)

Run

Output

[ 5.         11.66666667 18.33333333 25.        ]

Summary

In this tutorial of Python Examples, we created one-dimensional numpy array using different built-in functions.


Two dimensional array is an array within an array. It is an array of arrays. In this type of array the position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data.

In the below example of a two dimensional array, observer that each array element itself is also an array.

Consider the example of recording temperatures 4 times a day, every day. Some times the recording instrument may be faulty and we fail to record data. Such data for 4 days can be presented as a two dimensional array as below.

Day 1 - 11 12 5 2 
Day 2 - 15 6 10 
Day 3 - 10 8 12 5 
Day 4 - 12 15 8 6 

The above data can be represented as a two dimensional array as below.

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

Accessing Values

The data elements in two dimesnional arrays can be accessed using two indices. One index referring to the main or parent array and another index referring to the position of the data element in the inner array.If we mention only one index then the entire inner array is printed for that index position.

Example

The example below illustrates how it works.

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

print(T[0])

print(T[1][2])

Output

When the above code is executed, it produces the following result −

[11, 12, 5, 2]
10

To print out the entire two dimensional array we can use python for loop as shown below. We use end of line to print out the values in different rows.

Example

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
   for c in r:
      print(c,end = " ")
   print()

Output

When the above code is executed, it produces the following result −

11 12  5 2 
15  6 10 
10  8 12 5 
12 15  8 6 

Inserting Values

We can insert new data elements at specific position by using the insert() method and specifying the index.

Example

In the below example a new data element is inserted at index position 2.

from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T.insert(2, [0,5,11,13,6])

for r in T:
   for c in r:
      print(c,end = " ")
   print()

Output

When the above code is executed, it produces the following result −

11 12  5  2 
15  6 10 
 0  5 11 13 6 
10  8 12  5 
12 15  8  6 

Updating Values

We can update the entire inner array or some specific data elements of the inner array by reassigning the values using the array index.

Example

from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

T[2] = [11,9]
T[0][3] = 7
for r in T:
   for c in r:
      print(c,end = " ")
   print()

Output

When the above code is executed, it produces the following result −

11 12 5  7 
15  6 10 
11  9 
12 15 8  6 

Deleting the Values

We can delete the entire inner array or some specific data elements of the inner array by reassigning the values using the del() method with index. But in case you need to remove specific data elements in one of the inner arrays, then use the update process described above.

Example

from array import *
T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]

del T[3]

for r in T:
   for c in r:
      print(c,end = " ")
   print()

Output

When the above code is executed, it produces the following result −

11 12 5 2 
15 6 10 
10 8 12 5 

How do you create a dimensional array?

You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

How do you create an N dimensional array in Python?

N-dimensional array.
Example-1 >>> import numpy as np >>> a = np.array([[3, 4, 5], [6, 7, 8]], np.int32) >>> a.shape (2, 3) >>> a.dtype dtype('int32') ... .
Example - 2 >>> # The element of a in the *second* row, *third* column, namely, 6. >>>.

How do you create a 4 dimensional array in Python?

1 Answer.
a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]).
a = np.expand_dims(a, axis=0).
a = np.repeat(a, 4, axis=0).

How do you create an array in Python?

In Python, you can create new datatypes, called arrays using the NumPy package. NumPy arrays are optimized for numerical analyses and contain only a single data type. You first import NumPy and then use the array() function to create an array. The array() function takes a list as an input.