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. 1. Adding a New Element in the Outer Array. arr1.insert(ind,arr_insert) 2. 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
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)
Nested lists (Two dimensional lists for Python)

Nested lists (Two dimensional lists for Python)

  • 1.
  • 2.
  • 3.
    • 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.
  • 4.
    • A 2Darray 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?
  • 5.
    Syntax of Python2D 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.
  • 6.
    Syntax of Python2D 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] ]
  • 7.
    Accessing Values inPython 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
  • 8.
    • If anindex 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.
  • 9.
    Traversing Values inPython 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.
  • 10.
  • 11.
    Inserting Values inPython 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. 1. Adding a New Element in the Outer Array. arr1.insert(ind,arr_insert) 2. Adding a New Element in the Inner Array. arr1[r].insert(ind,arr_insert)
  • 12.
    Example of insertingvalues to outer value
  • 14.
    Inserting value tothe inner list
  • 15.
    Updating Values inPython 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
  • 16.
    Updating inner list •If you want to update inner list • new_arr=[10,11,12] • arr[1]=new_arr