Python Forum

Full Version: Q. Nested for statement in list type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, i'm beginner python, this is my source

tList = [[None]*3]*9

for j in range(0, 9):
    for k in range(0, 3):
        if(k==0 and j==0):
            tList[j][k] = 2
        elif(k==1):
            tList[j][k] = 4
        elif(k==2):
            tList[j][k] = 5
    print(j, k)
print(tList)
I don't understand result.
j count increase is ok in for statement
Especially, why changed '2' in tList[1][0] ?
plz help me.
what don't you understand?
did you mean to include the first print inside the first loop?
(Mar-14-2018, 03:38 AM)Larz60+ Wrote: [ -> ]what don't you understand? did you mean to include the first print inside the first loop?

I guess like this
[[2,4,5][None,4,5]...]
By
line 5 : if(k==0 and j==0):

But, result
[[2,4,5][2,4,5]...]
Please learn how to use code tags read BBCODE