-1

I commented out and located where the weird bug starts occurring in the code. It's supposed to be a sliding window problem, and as I slide the window it's supposed to add the window list inside the results list. But for some reason as I update the window list it seems to also be updating the results list??? but it updates at a point when I don't even mention the results list?

def find_subarrays(arr, target):
  result = []
  start = 0
  window = []
  product = 1
  print("target", target)
  for end in range(len(arr)):
    print("check Result", result)
    print("arr[end]", arr[end])
    window.append(arr[end]) # Somehow the variables window and result are connected
    print("check Result 2", result, "hmm",window)

    print("firstR ", result)
    result.append(window)
    print("secondR ", result)
    
    # print("result", result)

  return result

find_subarrays([2, 5, 3, 10], 30)

Re. the suggested duplicate:

I am not doing an multiplication on the result list and I removed a good chunk what they actual code is supposed to do, in order to simplify and focus on the root of the problem.

also with each loop the window list is change because it is adding each element of the input arr and at the end of each loop it adds it to the result array. The output I'm getting at the end is the the same array multiple time? but the change happens at a weird spot where I don't even modify the result array.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Os-
  • 13
  • 1
  • 1
    You keep appending the same list to `result`, so when you modify one, you modify them all. – Pranav Hosangadi Nov 06 '21 at 04:59
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Pranav Hosangadi Nov 06 '21 at 05:00
  • 1
    _"I am not doing an multiplication on the result list"_: but the result is the same: When you do `result.append(window)`, `window` is still _the same object from one iteration to the next. Appending an element to the list doesn't create a copy. – Pranav Hosangadi Nov 06 '21 at 05:14
  • @PranavHosangadi I think I understand what you are saying about the object, but I've been doing this for a long time and I haven't run into this issue before which is very weird? Like I'm sure I've done this exact task before multiple time and not run into this problem. thank you for further explaining Pranav. – Os- Nov 06 '21 at 05:19
  • Then you've been missing bugs for a long time ¯\\_(ツ)_/¯. This exact thing happens in every version of python I've used (since python 2) – Pranav Hosangadi Nov 06 '21 at 05:22

1 Answers1

2

You are using window variable to append in result list. That is causing this snyc in result. You can use below snippet to solve this issue.

result.append([*window])

  • 1
    [here is the same topic](https://discuss.python.org/t/confusing-behaviour-while-using-append-in-a-function/3429) When you use a variable(of mutable data type) to append in the list. the resultant list references to same variable instead of creating copy of data from initial variable(mutable data type). – Simranjot Sandhu Nov 06 '21 at 05:19