⊗pyPmMdLTDI 169 of 208 menu

Iterating over a 3D list in Python

To iterate over a three-dimensional list, you need to use three nested loops. Let's say we have a list like this:

lst = [ [ ['a', 'b'], ['c', 'd'], ], [ ['e', 'f'], ['g', 'h'], ] ]

Let's display all its elements on the screen:

for sub in lst: for sub_el in sub: for el in sub_el: print(el)

A three-dimensional list is given:

lst = [ [ ['q', 'w', 'e'], ['r', 't', 'y'], ['u', 'i', 'o'], ], [ ['p', 'a', 's'], ['d', 'f', 'g'], ['h', 'j', 'k'], ], [ ['l', 'z', 'x'], ['c', 'v', 'b'], ['n', 'm', 'q'], ], ]

Use a loop to print all the items in the list to the console.

A three-dimensional list is given:

lst = [ [ [1, 3], [5, 7], ], [ [2, 4], [6, 8], ], ]

Use a loop to get the sum of all the elements in a list.

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