We go through the code, should we:
x = []
: create an empty list named x
for i in range(3)
: repeat the code below three times:x = [x + x]
reassign the x
value to the current x
value plus the current x
value and wrap it in a list.
Essentially, each iteration happens in the fact that your list is nested one level, and the list inside your list is currently becoming a subordinate list of this nested list.
In fuzzy terms, the simplest explanation would be: at each iteration of the for loop, the value of the variable x is assigned to the value of two nested, which can also contain two other nested lists within the same list.
Here is an illustration showing the value of x at each iteration for
-loop:
>>>
If the above illustration is too crowded, here is a shorter one:
First iteration Value of x: [[]] Second iteration Value of x: [[[], []]] Third iteration Value of x: [[[[], []], [[], []]]]
I also find creating a tree type structure from a nested list, helps to understand what happens better:
[ # 1st level [ # second level [ # third level [], # fourth level [] ], [ [], [] # fourth level ] # third level ] # second level ] # 1st level
source share