learn kro favicon img

Python 2D Arrays: Two-Dimensional List Examples

Array is a data structure used to store elements. An array can only store similar types of elements. A Two Dimensional is defined as an Array inside the Array. The index of the array starts with 0 and ends with a size of array minus 1. We can create ‘n’ number of arrays in an array.

How to Create Array in Python?

We can create a two-dimensional array(list) with rows and columns.

Syntax:

Where,

r stands for rows and c stands for columns

Example: Following is the example for creating

2D array with 4 rows and 5 columns

Output:

Accessing the values

We can access the values using index position

Syntax:

We can get row value using [] operator

We can get column value using [][]

where,

  • array is an input array
  •  row index is the row index position starts from 0
  •  column index is the column index position starts from 0 in a row.

Example:

In this example we are going to access the values using index positions

Output:

We can also access elements using for loop

Syntax:

where,

  • rows are used to iterate the row by row
  • columns is used to iterate the values present in each row.

Example:

Output:

Inserting the values into the two-dimensional array

Here we are going to insert values into two dimensional array using insert() function

Syntax:

where,

  • the array is the input array
  • the index is the row position to insert a particular row
  • value are the values to be inserted into the array

Example: Insert to values in the array

Output:

Updating the values into the two-dimensional array

Here are two methods for updating values in the 2-D array(list).

You can update rows by using the following syntax

You can update column values inside rows by using the following syntax

Example:

Output:

Deleting the values from two-dimensional array

You can delete rows using the del function

Syntax:

where,

  • the array is the input array
  • index refers to the row index

Example:

Output:

Get the size of two-dimensional array

You can get the size of the two-dimensional array using the line() function. It will return the number of rows in the array

Syntax:

Example:

Get the length of the two-dimensional array

Output:

Summary:

Here are some important Array(list) Methods

MethodDescriptionSyntaxExample 
Append():This method helps us to add an element at the end to an existing array.  array.append(value)  # Adding an element using append method to the end of an array array=[1,2,3]      array.append(4)      print(array)    Output:[1,2,3,4]  
Clear():This method helps us to remove all the elements present in an arrayarray.clear()# Removing all the elements from an array array=[1,2,3] array.clear()Output:[]
Copy():This method helps us to copy the contents of an array to a new arrayarray1=array.copy()#Copying the elements from an array to a new array array=[1,2,3] array1=[] array1=array.copy() print(array1)  Output:[1,2,3]
Count():This method helps us to count the no.of occurences of a an specified element in an arrayarray.count(element)#Counting the no of times an element is present in an array array=[1,2,3] print(array.count(8)) Output: 0
Extend():This method helps us to extend an array beyond it’s consisting elements.array.extend(array1)#Extending an existing array with another array array=[1,2,3] array1=[4,5,6] array.extend(array1) print(array)Output: [1,2,3,4,5,6]
Index():This method helps us to find the index of an element in an arrayarray.index(element)#returing the index an element in an array array=[1,2,3] print(array.index(3))Output:2
Insert():This method helps us to insert elements into an array at specified index.array.insert(index,element)#Inserting an element at a specified index into an array array=[1,2,3] array.insert(2,4) print(array)Output:[1,2,4,3]
Pop():This method helps us to remove an element at specified indexarray.pop(index)#Removing an element at specified index through pop method array=[1,2,3] array.pop(2) print(array)Output:[1,2]
Remove(): This method helps us to remove an particular matching element in an array.array.remove(element)array=[1,2,3] array.remove(2) print(array)Output:[1,3]
Reverse():This method helps is to reverse the elements in an array.array.reverse()array=[1,2,3,4] array.reverse() print(array)Output:[4,3,2,1]

Leave a Comment

Your email address will not be published. Required fields are marked *