Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About list and method ....
#1
hello, is there someone have ever facing same situation like me ? lets say i have write a code as following :

level_1 = []
level_2 = []

for t in range(2):

    for f in range(2):
        level_0 = ["x" for r in range(3)]
        print("level_0 =",level_0)
        level_1.append(level_0)
        print("level_1 =",level_1)
        
    level_2.append(level_1)
    print("level_2 =",level_2)
and the output become:
level_2 =[[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']], [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
my question is :
why the output level_2 become 2 x 4 x 3 element ?

Because from my thought is like this :

-for level_0 for-loop it will fill the level_0 list with element 'x' each time the for level_0 command is executed, because the loop-for require 3x looping (in range 3) so the list level_0 becomes level_0 = ['x', 'x', 'x']
-and the program execute next command which is level_1.append(level_0) that means every time this command executed, the level_1 list will fill with an element level_0 that already contain 3x element "x" before, because the for-loop on level_1 require 2x (in range 2) looping then level_1 list becomes, level_1 = [['x', 'x', 'x'], ['x', 'x', 'x']] and program will break off from the loop
-next command is level_2.append(level_1), that means level_2 list will be filled by the level_1 list as an object/element, then the level_2 list will becomes like this : list_2 = [[['x', 'x', 'x'], ['x', 'x', 'x']]]
-Because the for-loop level_2 require 2x looping so the program heading back to the top and do the for-loop level_1 and level_0 once more

-level_0 list will be refill again with 3 element "x" and become level_0 = ["x","x","x"]
-and the program will fill the level_1 list that already have 2 object/element level_1 before, the loop require 2x (in range 2) looping so the list will become, level_1 = [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']] === > level 1 list that already contain 2 element/object will be added by 2 more element
-last the level_1 list will be added to level_2 list that already contain 1 element before, so the end output will become,

level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x']],[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
=== > this is from my thoughts, but the Python output is :

level_2 = [[['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']], [['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x']]]
can someone explain to me why my analysis is wrong ?

Note : Sorry if you get confuse from my writing because the lack of my English language ability, but i hope you can understand what the point of my question is... Cool , tq
Reply


Messages In This Thread
About list and method .... - by Fernando_7obink - Dec-22-2020, 03:46 AM
RE: About list and method .... - by bowlofred - Dec-22-2020, 04:02 AM
RE: About list and method .... - by Fernando_7obink - Dec-22-2020, 09:15 AM
RE: About list and method .... - by deanhystad - Dec-22-2020, 04:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about List.reverse() method tomliuwhite 1 1,360 Dec-07-2021, 08:20 AM
Last Post: ndc85430
  Unable to use random.choice(list) in async method spacedog 4 3,453 Apr-29-2021, 04:08 PM
Last Post: spacedog
  print all method and property of list object engmoh 4 2,872 Oct-26-2019, 05:33 PM
Last Post: engmoh
  why my method doesn't find my List in the same class? Scorpio 2 2,394 Jan-31-2019, 05:21 PM
Last Post: Scorpio
  [Python Class] save method output to global file/list Vijay 3 5,074 Dec-23-2017, 03:20 AM
Last Post: Vijay

Forum Jump:

User Panel Messages

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