Python Forum
Q. Nested for statement in list type - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Q. Nested for statement in list type (/thread-8943.html)



Q. Nested for statement in list type - ives - Mar-14-2018

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.


RE: Q. Nested for statement in list type - Larz60+ - Mar-14-2018

what don't you understand?
did you mean to include the first print inside the first loop?


RE: Q. Nested for statement in list type - ives - Mar-14-2018

(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]...]


RE: Q. Nested for statement in list type - Larz60+ - Mar-14-2018

Please learn how to use code tags read BBCODE