⊗pyPmCoTDLG 31 of 128 menu

Generating a Two-Dimensional List in Python

Using two loops specified in a comprehension, you can create a two-dimensional list.

Let's create a list consisting of three lists, which in turn contain numbers from 1 to 4. To do this, we need to make another inner comprehension within the comprehension. In it, we will specify the generation of numbers using a loop and the variable j. At the same time, the outer loop with i will be placed to the right of the inner comprehension:

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

Code execution result:

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

Using a comprehension, create the following list:

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

Using a comprehension, create 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], ], ]
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline