0

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

rioV8
29.5k4 gold badges47 silver badges65 bronze badges
asked Jan 10, 2022 at 15:39
5
  • Could you format your list so that it would be easier to perceive? Commented Jan 10, 2022 at 15:43
  • @UMR thanks for your comment, what do you mean by formatting my list? I am coping the exact result from my visual studio code interpreter ! Commented Jan 10, 2022 at 15:46
  • Running your code with the given 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 Commented Jan 10, 2022 at 15:58
  • visual studio code is not a python interpreter Commented Jan 10, 2022 at 16:30
  • @rioV8 thanks for the comment, I haven't made such declaration in my post. I only refered to it, as the interperter that I used, whilst running the code. Thanks again, appreciate your efforts. Commented Jan 12, 2022 at 12:24

1 Answer 1

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']]
 ]
]
answered Jan 10, 2022 at 16:23
3
  • 1
    Thanks @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? Commented 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 "). Commented Jan 10, 2022 at 22:14
  • 1
    Indeed, that's probably why it didn't work. Thanks for the input, much appreciated. Commented Jan 11, 2022 at 10:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.