AppDividend
Latest Code Tutorials

# How to Implement Python 2D Array with Example

In every programming language, an array is represented as an array[index]. The index is the number that states the location number of a particular item in the memory location.

Since array uses sequential memory, therefore the index numbers are also continuous. It is quite obvious to note that the array indexing starts at 0 and ends at n-1, where n is the size of an array.

## Python array

An array in Python is a linear data structure that contains an ordered collection of items of the same data type in the sequential memory location. There is no specific array object in Python because you can perform all the operations of an array using a Python list.

We can define the list as a single-dimensional array using the following syntax.

`array_name = [ ]`

You can initialize the Python array using the following code.

```# app.py

data = [11, 21, 19, 18, 46]
print(data)```

#### Output

`[11, 21, 19, 18, 46]`

Python does all the array related operations using the list object.

## Implement Python 2D Array

To implement a 2D array in Python, we have the following two ways.

1. Use a list object as a 2D array.
2. Use the numpy library to create a two-dimensional array.

A two-dimensional array in Python is an array within an array.

### Use a list object as a 2D array.

To define a 2D array in Python using a list, use the following syntax.

```arr = [[], []]
```

Let’s declare a 2D array with initial values.

```# app.py

arr = [[11, 21], [19, 46]]
print(arr)```

#### Output

`[[11, 21], [19, 46]]`

### Use the numpy library to create a 2D array

If you have not installed numpy, then you need to install it first.

To install a numpy library, use the following command.

`python3 -m pip install numpy`

Then you need to import the numpy.

```# app.py

import numpy as np```

Now, let’s define and declare a 2D array using numpy.

```# app.py

import numpy as np

arr2D = np.array([[19, 21], [11, 46]])
print(arr2D)
print(type(arr2D))
```

#### Output

```[[19 21]
[11 46]]
<class 'numpy.ndarray'>```

These are the main two ways to create 2D arrays in Python.

## Access elements in a 2D array

To access the elements, use two indices, which are represented by rows and columns of the array. The first index to define the location of the list where our element is stored and the second index to define the location of an element in that list or array.

```# app.py

import numpy as np

arr2D = np.array([[19, 21], [11, 46]])
print(arr2D)
```

#### Output

`21`

The array index starts at 0. So in our code, 0(row) means the first indexed item, and then 1(column) means the second element of that item, which is 19. So it returns 19.

## How to insert elements in a 2D array

To insert elements in Python 2D array, use the append() methodThe append() method will only work if you have created a 2D array using a list object.

```# app.py

arr2D = [[19, 21], [11, 46]]
print(arr2D)

newElement = [18, 36]

print('After appending a new element')
arr2D.append(newElement)
print(arr2D)
```

#### Output

```[[19, 21], [11, 46]]
After appending a new element
[[19, 21], [11, 46], [18, 36]]```

The append() method adds the single item to the existing array. In our case, it is a single array. The append() method doesn’t return a new array; instead, it modifies the original array. It will return None if you try to save in the different variable and then print that variable.

## Insert an element at the specific index in the 2D array

To insert an element at the specified index, you need to specify the index while appending a new element.

```# app.py

arr2D = [[19, 21], [11, 46]]
print(arr2D)

newElement = [18, 36]

print('After appending a new element at specified position')
arr2D.append(newElement)
print(arr2D)```

#### Output

```[[19, 21], [11, 46]]
After appending a new element at specified position
[[19, 21], [11, 46, [18, 36]]]```

In this example, we are not adding the third element in the 2D array. Instead, we are adding the third element of the second element of the array.

## How to update elements of a 2D array in Python

To update the element of the 2D array, use the following syntax.

```arr[index_of_sub_array][ index_of_element_to_update ] = new_value
```

See the following example.

```# app.py

arr2D = [[19, 21], [11, 46]]
print(arr2D)

print('After updating with a new element at specified position')
arr2D = 18
print(arr2D)
```

#### Output

```[[19, 21], [11, 46]]
After updating with a new element at specified position
[[19, 18], [11, 46]]```

In this example, we want to replace `21` element with `18`. The index of `21` is `[0, 1]`. So we explicitly tell the PythonPython to replace the element of this index[0, 1] with a new element(18).

## How to remove elements from a 2D array in Python

To remove an element from the array, use the pop() method. I am assuming that the array is created as a list; otherwise, the pop() method won’t work.

```# app.py

arr2D = [[19, 21], [11, 46]]
print(arr2D)

print('After removing a the element at specified position')
arr2D.pop(1)
print(arr2D)
```

#### Output

```[[19, 21], [11, 46]]
After removing a the element at specified position
[[19, 21]]```

The pop() removes the element at the specified position and returns the deleted item.

However, in some instances, we have to delete the particular item instead of the complete array. To do that, use the following syntax.

`array.pop(index_of_sub_array_to_remove)`

See the following code for a better understanding.

```# app.py

arr2D = [[19, 21], [11, 46]]
print(arr2D)

print('After removing the element at specified position')
arr2D.pop(0)
print(arr2D)
```

#### Output

```[[19, 21], [11, 46]]
After removing the element at specified position
[[19, 21], ]```

In this example, we want to remove the 11 element whose index is [1, 0]. So we are explicitly telling the array that removes that specified element.

## Conclusion

Creating, Updating, and Removing items from Python 2D array is easy but it totally depends on how you are defining and declaring the array. If you are assuming the list as an array then performing crud operation on them is different then performing the crud operation on numpy 2D array. That is it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.