how the code works ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

how the code works ?

n = 2 a = [[0] * n for i in range(n)] Output is --> [[0,0] , [0,0]] Please explain the output how it has come ?

23rd Sep 2018, 11:57 AM
🌷Ⓛⓤ𝔹𝓝ⓐ Ⓚⓗ𝔸ⓝ🌷
🌷Ⓛⓤ𝔹𝓝ⓐ Ⓚⓗ𝔸ⓝ🌷 - avatar
1 Réponse
+ 1
a = [[0]*2 for i in range(2)] [0]*2 = [0]+[0] = [0,0] range(2) contains 2 numbers so [0,0] is used twice. result is a list containing [0,0] twice.
23rd Sep 2018, 12:07 PM
davy hermans
davy hermans - avatar