Reputation: 35
I developed long logic to arrive at certain result, from which I can append the nested list with nested-non list element. very simple process but the result I am having in my interpreter is very bizarre. Thus, I wanted to come here to get useful insights, on how to solve such problem.
To explain my code in technical terms, here is what I mean:
I have a variable called result, in which, if printed, I get the followings:
Input:
print(result)
output:
[[['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']]], [['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']]]]
if I apply for loop, to append the nested list, my input will be:
for i in result:
for x in i:
x[-1].append(x[0])
print(result)
The outcome:
[[['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2', '1']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2', '1']]], [['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2', '1']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2', '1']]]]
The desired outcome, that I am aiming to achieve:
[[['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '1']]], [['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '2']], ['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1', '1']]]]
I was only able to get the desired result by creating a new interpreter file with new variable, coping the output of result, from the current file to get the desired result. So, what is happening with my logic to cause such problem?
Please note, it is not a repetitive question, I am not asking how to solve it technically; I am questioning what causing this behaviour within the code to give me two different results applying same logic in two files?
please also note, I am using visual studio code as my interpreter
Upvotes: 0
Views: 95
Reputation: 2029
You are appending the first value of the list x to the previous entry.
This happens because Python lists are mutable (and so can be edited/updated) and you're updating the list x[-1] that itself is an entry of list x that is an entry of list i that is an entry of list result.
for i in result:
for x in i:
breakpoint() # breaks at the first x in the first i list to evaluate what happens
# x = ['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']
# x[0] = '2'
# x[-1] = ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']
x[-1].append(x[0])
print(result)
As result i took:
result = [
[
['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']],
['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']]
],
[
['2', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']],
['1', ['Length_PanelSet_2_Plan_2', 'Length_PanelSet_1_Plan_1_with_Hinged_Comments', 'Length_PanelSet_1_Plan_1']]
]
]
Upvotes: 1