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
1 Answer 1
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']]
]
]
-
1Thanks @Frank, that was very helpful explanation. Just wanted to ask you about BreakPoint(), when I applied it to the code, I had a syntax error! is there any module I need to import to get it to work with no issues?Pythonic_Architect– Pythonic_Architect01/10/2022 20:17:38Commented Jan 10, 2022 at 20:17
-
Maybe you are using pre 3.7 Python version. You can use "import pdb; pdb.set_trace()" instead (without ").Frank– Frank01/10/2022 22:14:04Commented Jan 10, 2022 at 22:14
-
1Indeed, that's probably why it didn't work. Thanks for the input, much appreciated.Pythonic_Architect– Pythonic_Architect01/11/2022 10:33:55Commented Jan 11, 2022 at 10:33
result
structure generates your desired output. It is possible that you are shallow copying one of the inner lists at some point in your long logic. Please check out this question