Python Forum
Q. Nested for statement in list type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Q. Nested for statement in list type
#1
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.
Reply
#2
what don't you understand?
did you mean to include the first print inside the first loop?
Reply
#3
(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]...]
Reply
#4
Please learn how to use code tags read BBCODE
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert set to a list type in python firaki12345 2 1,699 Feb-05-2021, 03:45 PM
Last Post: buran
  error when inserting list statement from python to MySQL ADBYITMS 0 1,542 Nov-10-2019, 10:44 AM
Last Post: ADBYITMS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020