⊗pyPmCoMLC 30 of 128 menu

Multiple Loops with Condition in List Comprehension in Python

A condition can be added to multiple loops specified in a comprehension. Let's set a condition so that j is less than i:

lst = [(i, j) for i in range(1, 4) for j in range(1, 3) if j < i] print(lst)

After executing the code, the following list will be returned:

[(2, 1), (3, 1), (3, 2)]

What will be the result of executing the following code:

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

What will be the result of executing the following code:

lst = [(i, j) for i in range(1, 4) for j in range(1, 4) if i % 2 == 0] print(lst)
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