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
  • 24,506
  • 3
  • 32
  • 49
  • Could you format your list so that it would be easier to perceive? – UMR Jan 10 '22 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 ! – Pythonic_Architect Jan 10 '22 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](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – shriakhilc Jan 10 '22 at 15:58
  • visual studio code is not a python interpreter – rioV8 Jan 10 '22 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. – Pythonic_Architect Jan 12 '22 at 12:24

1 Answers1

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']]
    ]
]
Frank
  • 1,959
  • 12
  • 27
  • 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? – Pythonic_Architect Jan 10 '22 at 20:17
  • Maybe you are using pre 3.7 Python version. You can use "import pdb; pdb.set_trace()" instead (without "). – Frank Jan 10 '22 at 22:14
  • 1
    Indeed, that's probably why it didn't work. Thanks for the input, much appreciated. – Pythonic_Architect Jan 11 '22 at 10:33