# Changes in for-loop don't apply outside loop

**URL:** <https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670>\
**Category:** General Usage\
**Created:** [February 15, 2023, 7:50am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670 "2023-02-15T07:50:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [February 15, 2023, 7:50am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670/1 "2023-02-15T07:50:56Z")

</div>

```julia
value = # A custom array I've made with dictionaries in them. Pretend original price values were [1.0, 5.0, 7.0, 9.0, 12.0]. 

for cur_perm in perm_array

  println(cur_perm) # looks something like [2.0, 10.0, 20.0, 30.0, 40.0] for this iteration lets pretend

  dupl_of_value = copy(value) 

  for jj in eachindex(cur_perm) 
    
    dupl_of_value[jj]["price"] = cur_perm[jj] 
    println(dupl_of_value[jj]["price"]) # prints out the new value, e.g. 2.0 when jj=1, which is correct and what I want it to do

  end 

  println(dupl_of_value[1]["price"]) # prints out the original value of 1.0, which is incorrect and not what I want it to do 

end 

```

Hi all,

Above is a snippet from my program/script. In general, the program generates permutations and is supposed to apply them all as new entries in a dictionary. My program is generating every permutation from the data it is given correctly, however, it is not applying them all. The above snippet looks to be roughly where it goes wrong for me.

Inside the second FOR loop, I overwrite the “price” key at index jj of dupl\_of\_value with the matching jj value from cur\_perm, and this works fine in the print statement immediately after. However, when the second FOR loop ends, none of these changes seem to persist, as the “price” key at index 1 of dupl\_of\_value is not the correct value when I print it.

Would anybody have any suggestions as to why this might be the case please? I imagine it’s probably some simple mistake I’m making with scope, but I’m yet to spot it and it’s left me stumped for a bit. Alternatively, maybe I’m missing some idiosyncrasy with Julia?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 15, 2023, 9:30am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670/2 "2023-02-15T09:30:51Z")

</div>

Works as expected.

```julia
julia> value = [Dict("price"=>x) for x in [1.0, 5.0, 7.0, 9.0, 12.0]]
       perm_array = [[2.0, 10.0, 20.0, 30.0, 40.0]]
       for cur_perm in perm_array
           println(cur_perm)
           dupl_of_value = copy(value)
           for jj in eachindex(cur_perm)
               dupl_of_value[jj]["price"] = cur_perm[jj]
               println(dupl_of_value[jj]["price"])
           end
           println(dupl_of_value[1]["price"])
       end
[2.0, 10.0, 20.0, 30.0, 40.0]
2.0
10.0
20.0
30.0
40.0
2.0

```

Are you sure you’re not mistaking something else?

For example: `copy` creates a copy of the `value` container, but it doesn’t copy the data inside it—all the entries point to exactly the same dictionaries; `all(splat(≡), zip(value, dupl_of_value))` is `true`. If you want the contents to be copied recursively, you need to use `deepcopy` instead.

---

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [February 16, 2023, 1:44am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670/3 "2023-02-16T01:44:58Z")

</div>

```julia

println(value) # I can include a snippet of the structure of value if necessary
# In the original `value` variable, all of the values for ["price"]["price"] are 200.0

for good_perm in filtered_valid_perm_array
    
  println(good_perm)
  
  dupl_of_value = deepcopy(value)
    
  for jj in 1:(length(good_perm))
    dupl_of_value[jj]["price"]["price"] = good_perm[jj]
    
    if jj == 1
      println(string("good: ", dupl_of_value[jj]["price"]["price"]))
    end

  end
    
  println(string("1 bad: ", dupl_of_value[1]["price"]["price"]))
  println(string("2 bad: ", dupl_of_value[2]["price"]["price"]))
  # printing indexes 3, 4 and 5 seem to be fine, although I may have noticed 
  # that printing index 2 can show it as incorrect on some iterations,  
  # though I may be misremembering. 

  # More code after this 

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/4/b47b19d4cce410723c427023402d46d0749bfc5d.png)

Hi there,

Thanks for your reply. I previously was using deepcopy instead of copy but I was still running into the same issue. I’ve re-done my snippet using deepcopy and made some other small changes/expansions too. Probably the main difference is that the “price” key is nested twice in the dupl\_of\_value dictionary; I left it at one nesting in the first snippet for simplicity and because I didn’t think it’d have any impact but maybe I’m wrong?

I’ve also included a snippet of the output I’m receiving when I run my script. The intention is to have “1 bad” match “good”. I’ve noticed that when “1 bad” does not match “good”, the value for “1 bad” is instead the value of the 5th index in the good\_perm array. Based on this, whenever the values of the 1st index and 5th index of good\_perm are the same, then “1 bad” and “good” do match (as we’d expect, though I suspect this is coincidence and doesn’t explain why good\_perm index 5 is assigned at dupl\_of\_value index 1).

Would anybody have any guess as to why the value of dupl\_of\_value[1][“price”][“price”] is taking on the value of good\_perm[5] when outside of the inner FOR loop, but not when inside of the inner FOR loop?

Hope this all makes sense, thanks all.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 16, 2023, 6:36am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670/4 "2023-02-16T06:36:23Z")

</div>

Works as expected.

```julia
julia> value = [Dict("price"=>Dict("price"=>x)) for x in [200.0, 200.0, 200.0, 200.0, 200.0]]
       filtered_valid_perm_array = [[2.0, 10.0, 20.0, 30.0, 40.0]]
       for good_perm in filtered_valid_perm_array
       println(good_perm)
       dupl_of_value = deepcopy(value)
           for jj in 1:length(good_perm)
           dupl_of_value[jj]["price"]["price"] = good_perm[jj]
               if jj == 1
                   println("good: ", dupl_of_value[jj]["price"]["price"])
               end
           end
           println("1 bad: ", dupl_of_value[1]["price"]["price"])
           println("1 bad: ", dupl_of_value[2]["price"]["price"])
       end
[2.0, 10.0, 20.0, 30.0, 40.0]
good: 2.0
1 bad: 2.0
1 bad: 10.0

```

Are you sure you’re not mistaking something else?

Can you make a minimum working example (MWE) of _runnable_ code that shows the issue you’re concerned about, as described here: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)?

---

<div class="post-metadata">

**Author:** ![Name1](https://avatars.discourse-cdn.com/v4/letter/n/ed655f/32.png) [@Name1](https://discourse.julialang.org/u/Name1)\
**Post date:** [February 17, 2023, 6:37am UTC](https://discourse.julialang.org/t/changes-in-for-loop-dont-apply-outside-loop/94670/5 "2023-02-17T06:37:59Z")

</div>

Hi there,

Thanks for your response and the clarification on the MWE. In the process of making and testing the MWE, I’ve fixed my issue and got the code behaving as I need it too. Basically, the issue didn’t have to do with how I made and used the permutations, but with how I constructed the original `value` variable that dupl\_of\_value then deep copied (my snippets constructed the original `value` variable different to how I actually am constructing them hence why for you the code was working; I didn’t think how it was constructed at the time mattered when making my snippets but now I know better from testing and the lesson on MWE). Originally, I assigned index 5 of value to be the same as index 1 without using deepcopy, so this got them to share the same memory address. Thus, when jj hit 5 and dupl\_of\_value[5][…] was modified, that also resulted in dupl\_of\_value[1][…] being modified.

Hope that makes some sense, thought I oughta explain what the issue was and how I’ve got it working now that I’ve identified it. Thanks for your help.
