Nested lists (Two dimensional lists for Python)

abzalbekulasbekov 43 views 24 slides Oct 07, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Two dimensional arrays


Slide Content

Two dimensional lists (arrays)

https://www.scaler.com/topics/2d-array-in-python/ https://snakify.org/en/lessons/two_dimensional_lists_arrays/ https://www.geeksforgeeks.org/python-using-2d-arrays-lists-the-right-way/

Two Dimensional (2D) lists in Python is a nested data structure, meaning it is a set of lists inside another list. The 2D list is mostly used to represent data in a tabular or two-dimensional format.

A 2D array in python is a  two-dimensional data  structure used for storing data generally in a tabular format. For example, if you want to store a matrix and do some computations, how would you do it in Python?

Syntax of Python 2D Array Each element in a 2D array is represented by two indices, the row and the column. 2D arrays in python are zero-indexed, which means counting indices start from zero rather than one; thus, zero is the first index in an array in python.

Syntax of Python 2D Array array_name=[[r1c1,r1c2,r1c3,..],[r2c1,r2c2,r2c3,...],. . . .] Where  array_name  is the array's name, r1c1, r1c1 etc., are elements of the array. Here r1c1 means that it is the element of the first column of the first row. A 2D array is an array of arrays. Example: # Define the two-dimensional list array_name = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

Accessing Values in Python 2D Array We can directly access values or elements of a 2D array in Python. It can be done by using the row and column indices of the element to be accessed. It has the following  syntax: array_name [ row_ind ][ col_ind ] EX: # Accessing elements array_name = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print("Element at row 1, column 1:", array_name [0][0]) # Output: 1 print("Element at row 2, column 3:", array_name [1][2]) # Output: 6 print("Element at row 3, column 2:", array_name [2][1]) # Output: 8

If an index exceeds the size, if the array is provided to this function, it returns the  index out of range error . Such indices are known as  out of bounds . 

Traversing Values in Python 2D Array Traversing in a 2D array in python can be done by using a for a loop. We can iterate through the outer array first, and then at each element of the outer array, we have another array, our internal array containing the elements. So for each inner array, we run a loop to traverse its elements.

Example of Traversing

Inserting Values in Python 2D Array Inserting a value in an array means adding a new element at a given index in the array and then shifting the remaining elements accordingly. Adding a New Element in the Outer Array. arr1.insert(ind,arr_insert) Adding a New Element in the Inner Array. arr1[r].insert(ind,arr_insert)

Example of inserting values to outer value

Inserting value to the inner list

Updating Values in Python 2D Array arr_name [r][c]= new_element It Single element and inner list update types Example: arr [1][2]=16 will update single element of list

Updating inner list If you want to update inner list new_arr =[10,11,12] arr [1]= new_arr
Tags