0

Every loop I'm calculating values of lists, outcomes are mostly wrong values. In trying to isolate the problem, I stumbled across something weird.

The changing of a value in a list

working_list[i] += value1

seems to have an effect on other lists.

"working_list" is a list that I need for further calculations, at the end of which I get "value2" which I use to adjust "result_list". I noticed, that result_list is not just changing in the last step of my calculation (at the end of the loop), but seems to change during the modification of the working_list. It makes me think I'm fundamentally misunderstanding how lists work.

A demonstration of the key lines (which to my understanding should be enough to see what my problem is):

result_list_empty=[]

for i in range(8760):
    results_list_empty.append(0)

results_list=results_list_empty

for n in range(years)
    working_list=results_list_empty
    for i in range(len(working_list)):

        print 'BEFORE '+str(sum(result_list))
        working_list[i] += value1
        print 'AFTER '+str(sum(result_list))

        value2=some_calulation(working_list)

        result_list[i]+=value2

returns

 BEFORE 3.97469913304
 AFTER 5.61311451171

To me this is confusing because code is supposed to work from top to bottom.. I'm not calling a function, just changing the value of a list member. So result_list changing doesn't make sense to me. Could somebody explain to me what's happening here?

Cheers

cheesus
  • 1,111
  • 1
  • 16
  • 44
  • 1
    [This video](https://www.youtube.com/watch?v=_AEJHKGk9ns) and [this article](https://nedbatchelder.com/text/names.html) explain the things you need to know better than any answer we could write up on the spot. – timgeb Oct 17 '18 at 08:56
  • 1
    how did you initialize lists? was it list_of_lists = [[] * N] ? this creates copies of lists. – Aleksei Maide Oct 17 '18 at 08:57
  • [mcve], please. – Aran-Fey Oct 17 '18 at 08:58
  • Is there a `Q_extra = depths` somewhere? – Vikrant Sharma Oct 17 '18 at 09:00
  • You'll have to share how you *created* the list(s). You may be using [assignment rather than a copy operation](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list), or you may be [creating multiple references to the same list with multiplication](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly), or you are [reusing a mutable object in a loop](https://stackoverflow.com/questions/35906411/list-on-python-appending-always-the-same-value). – Martijn Pieters Oct 17 '18 at 09:00
  • 1
    @AlekseiMaide No, it *doesn't* create copies, which is the crux of the problem. – Aran-Fey Oct 17 '18 at 09:01
  • @cheesus you should really provide your complete code or a minimal, complete and verifiable example, otherwise we can only make assumptions... – toti08 Oct 17 '18 at 09:02
  • I resolved this by using list_new=list(list_old) to copy the lists. Thanks @MartijnPieters – cheesus Oct 17 '18 at 12:44

0 Answers0