Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
row_num = int(input("Input number of rows: "))
col_num = int(input("Input number of columns: "))
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]

for row in range(row_num):
    for col in range(col_num):
        multi_list[row][col]= row*col

print(multi_list)


doubt:
I didn't understand line 3 of the above code.

What I have tried:

This question consists of use of list comprehension in Python.I cannot understand line 3.Kindly help.
Posted
Updated 28-Jul-20 22:37pm
v2

It's just declaring a 2d List.
See here: Two-dimensional lists (arrays) - Learn Python 3 - Snakify[^]
 
Share this answer
 
Comments
CPallini 29-Jul-20 4:38am    
5.
Quote:
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]


col_num = 3
for col in range(col_num)

=> for loop from 0 to 3 (0,1,2)

row_num = 4
for row in range(row_num)

=> for loop from 0 to 4 (0,1,2,3)

[0 for row in range(4)]

=> [0, 0, 0, 0]

Thus,
multi_list = [[0 for col in range(col_num)] for row in range(row_num)]

=> [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

OR (for easier read)
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

Defines a matrix of 4*3 with that line.
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900