⊗pyPmCoTDLG 31 of 128 menu

Generate a 2D List in Python

Using two loops defined in the inclusion, a two-dimensional list can be created.

Let's make a list consisting of three lists, which in turn contain numbers from 1 to 4. To do this, we need to make another internal inclusion in the inclusion. In it, we will write the generation of numbers using a loop and the variable j. In this case, we will define the external loop with i to the right of the internal inclusion:

lst = [[j for j in range(1, 5)] for i in range(0, 3)] print(lst)

Result of code execution:

[ [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], ]

Using the inclusion, make the following list:

[ [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], ]

Using the inclusion, make the following list:

[ [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], [ [1, 2, 3], [1, 2, 3], [1, 2, 3], ], ]
enru