Lecture-25 2d List_create 2024-25

COMP101 Introduction to Programming 2024-25

Lecture 25: 2-D Lists


Overview

  • Focus on creating 2-dimensional (2-D) lists.

  • Various methods of initialization and their implications.


List Dimensions

  • Definition: A 2-D list has multiple rows and columns.

  • Example: A list dimension of 4 x 7 has 4 rows and 7 columns (total of 28 elements).

  • Structure representation:

    • Rows and columns indexed in a grid format:

    0

    1

    2

    3

    4

    5

    6

    0,0

    0,1

    0,2

    0,3

    0,4

    0,5

    0,6

    1,0

    1,1

    1,2

    1,3

    1,4

    1,5

    1,6

    2,0

    2,1

    2,2

    2,3

    2,4

    2,5

    2,6

    3,0

    3,1

    3,2

    3,3

    3,4

    3,5

    3,6

  • Key Terms:

    • Dimension: Size of the 2-D list.

    • Element: A specific cell accessed via two indexes.

    • Addressing: Accessing elements in 'row-column order'.

      • First element: [0,0]

      • Last element: [3,6]

    • Homogeneous: All elements must hold the same data type.


Creating a 2-D List

  • Methods:

    1. Static Creation: Values defined by the programmer.

    2. Automatic Creation: Values generated by the software.

      • Techniques:

        • List comprehension (efficient for large lists).

        • Generator (suitable for very large lists).

        • Dynamic (not recommended due to side effects).


Different Types of 2-D Lists

  • Types:

    • Null List: All elements initialized to zero (most common).

    • Unary List: All elements initialized to one.

    • Dense List: Most elements carry a value.

    • Sparse List: Majority of elements are zero.

    • Diagonal List: Only diagonal elements have the same value.


Static Creation of Lists

Creating a Null List

  • Example of a 2x2 null list:

    null_list_1 = [[0], [0]]
    num_list_2 = [[0], [0]]
    num_list_both = [num_list_1, num_list_2]
    print(num_list_both)  # Outputs: [[0,0], [0,0]]
  • Revised better way:

    arrayNull = [[0, 0], [0, 0]]
    print(arrayNull)  # Outputs: [[0,0],[0,0]]

Additional Creation Examples

  • Example for a 3x4 Null List:

    list_3x4_null = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
    print(list_3x4_null)  # Outputs: [[0,0,0,0],[0,0,0,0],[0,0,0,0]]

Populating Lists

Populated from 1-D Lists

  • Example:

    num_list_1 = [1, 2, 3]
    num_list_2 = [4, 5, 6]
    num_list_both = [num_list_1, num_list_2]
    print(num_list_both)  # Outputs: [[1, 2, 3], [4, 5, 6]]

Creating Specific Dimensions

  • Example for a 2x3 List:

    list_2x3 = [[1, 2, 3], [4, 5, 6]]
    print(list_2x3)  # Outputs: [[1, 2, 3], [4, 5, 6]]

Advanced List Populating Methods

Using List Comprehension

  • Creates a null list:

    num_of_rows = 4
    num_of_cols = 4
    list_comp = [[0 for row in range(num_of_rows)] for col in range(num_of_cols)]
    print(list_comp)  # Outputs: [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

Using Generators for Null Lists

  • Example:

    list_gen = [[0] * num_of_cols for i in range(num_of_rows)]
    print(list_gen)  # Outputs a 4x4 list of nulls.

Diagonal Population of Lists

  • Populating diagonal elements:

    list_diag_4x4 = [[0 for row in range(4)] for col in range(4)]
    for i in range(4):
        list_diag_4x4[i][i] = 1
    for row in list_diag_4x4:
        print(row)  # Outputs the diagonal populated list.

Caution Against Dynamic Methods

  • Example (which should be avoided):

    list_null = [[0] * col] * row
  • This creates references, thus modifying one element affects all; this leads to unexpected results.