Python. Nested lists (2D arrays)

11.2.3.4 create nested lists
11.2.3.5 enter elements of nested lists from the keyboard

Python. Nested lists (2D arrays)


A nested list is a list that is an element of another list.


Lists can be nested at any level. We will consider only two-dimensional (2D) lists.

The 2D list is a data structure that can be represented in a square table. 

Each element of the list is also represented by a list (nested list).

To refer to an element in a 2D list, you should at first determine the index of the list element (row) and then determine the index of the nested list element (column).

For example, to get the value 8 from a table, you need to write the name of the list lst2D, then specify the index of the list element lst2D[1], then specify the index of the element in the nested list lst2D[1][2].

lst2D[1][2] = 8
lst2D[2][0] = 3

Question. Determine the result of a code snippet: print(lst2D[0][3], lst2D[2][2], lst2D[-1][-3])
  • Answer. 10 9 6

How to create 2D lists filled with zeros?

lst2D = [[0] * 10 for i in range(5)] 

Output:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

How to fill a 2D list with dimensions n*m from the keyboard?

If we want to input each value of an element in a new line:

lst2D = [] # create empty list
for i in range(3):
    row = [] # create empty list for new row in 2D list
    for j in range(4):
        row.append(input()) # each element is entered separately
    lst2D.append(row) # add new item (list) to 2D list

If we want to input each row of elements in line:

lst2D = []
for i in range(3):
    lst2D.append(input().split()) # input values of row in a space

If we want to input each row of integers on one line:

lst2D = []
for i in range(3):
    lst2D.append([int(x) for x in input().split()]) # convert each values to integer

How to process each element of a matrix?
Iterating over indices. We can get and change the value for each element

for i in range(n): # n rows
    for j in range(m): # m columns
        lst2D[i][j] = ... # change value of item with i-row and j-column

Iterating over elements. We can get value for each element

for row in lst2D: # iterate each element in list
    for col in row: # iterate each element in nested list
        print(col)

How to display all elements in a table?

for i in range(n):  # n rows
    for j in range(m): # m columns
        print(lst2D[i][j], end='\t') # output in row in a tab
    print()

or

for row in lst2D: # iterate each element in list
    for col in row: # iterate each element in nested list
        print(f"{col:5}", end='') # using f-string for alignment
    print()

Questions:

1. Explain what means 2D list/array.

2. Describe the main idea to process elements of the 2D list.

Exercises:

Ex. 1 Determine the result of code snippets

Ex. 2 Fill the blanks

Tasks:

 

 

Категория: Programming languages | Добавил: bzfar77 (17.01.2022)
Просмотров: 644 | Теги: Index, Row, column, Python, nested lists | Рейтинг: 5.0/2
Всего комментариев: 0
avatar