# How to store data from a nested for loop?

**URL:** <https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602>\
**Category:** New to Julia\
**Created:** [November 16, 2021, 5:53pm UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602 "2021-11-16T17:53:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [November 16, 2021, 5:53pm UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/1 "2021-11-16T17:53:57Z")

</div>

Hello!

I have a nested for loop, which should result in a vector of vectors. Unfortunately, I’m only getting the end result, rather than each iteration. I’m probably missing something obvious about nested for loops; minimum (not actually) working example below.

```julia
using DataFrames
# Replicate Minimum Working Data
#generate DataFrame names
dfnames = ["a", "b", "c", "d", "e", "f", "g", "h", "max", "years"]
#generate column Years data
years = collect(1:10)
#generate Vector DataFrames
dfvector = []
for i in years
    dfvector = push!(dfvector, DataFrame(hcat(rand(10,9), years), dfnames))
end

#Identify the maximum value for the first dataframe
maxvaluesyr1 = zeros(length(years))
for i in years
    maxvaluesyr1[i] = maximum(dfvector[1][dfvector[1].years .== i,:].max)
end

#Identify the maximum value for the second dataframe
maxvaluesyr2 = zeros(length(years))
for i in years
    maxvaluesyr2[i] = maximum(dfvector[2][dfvector[2].years .== i,:].max)
end

```

The above is what I would like, but I need to loop the “for i in years” for loop over each dataframe. I tried the below example, but I kept receiving the last j loop and not the first 9. Any help is incredibly appreciated and just let me know if I can clarify further!

```julia
maxvalues = zeros(length(years))
maxvaluesvector = []
for j=1:length(dfvector)
    for i in years
        maxvalues[i] = maximum(dfvector[j][dfvector[j].years .== i,:].max)
    end
    maxvaluesvector = push!(maxvaluesvector, maxvalues)
end

maxvalues = zeros(length(years))
maxvaluesvector = []
for j=1:length(dfvector)
    for i in years
        maxvalues[i] = maximum(dfvector[j][dfvector[j].years .== i,:].max)
        maxvaluesvector = push!(maxvaluesvector, maxvalues)
    end
end

maxvalues = zeros(length(years))
maxvaluesvector = repeat([maxvalues], length(dfvector))
for j=1:length(dfvector)
    for i in years
        maxvalues[i] = maximum(dfvector[j][dfvector[j].years .== i,:].max)
        maxvaluesvector[j] = maxes
    end
end

maxvalues = zeros(length(years))
maxvaluesvector = repeat([maxvalues], length(dfvector))
for j=1:length(dfvector)
    for i in years
        maxvalues[i] = maximum(dfvector[j][dfvector[j].years .== i,:].max)
    end
    maxvaluesvector[j] = maxes
end

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 16, 2021, 7:40pm UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/2 "2021-11-16T19:40:14Z")

</div>

Let’s take the first attempt:

> [@Mitch](#):
>
> ```julia
> maxvalues = zeros(length(years))
> maxvaluesvector = []
> for j=1:length(dfvector)
> for i in years
> maxvalues[i] = maximum(dfvector[j][dfvector[j].years .== i,:].max)
> end
> maxvaluesvector = push!(maxvaluesvector, maxvalues)
> end
> 
> ```

This allocates one `maxvalues` vector. For each value of `j`, the inner loop

```julia
for i in years
    maxvalues[i] = ...
end

```

overwrites the values in this vector. Then for each `j` the line

```julia
maxvaluesvector = push!(maxvaluesvector, maxvalues)

```

appends this vector `maxvalues` to `maxvaluesvector` (by the way the assignment is not necessary, you can write `push!(v, ...)` instead of `v = push!(v, ...)`).

The problem is that this always appends the same vector (the same container). You need to allocate a new vector for each `j`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 16, 2021, 7:41pm UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/3 "2021-11-16T19:41:03Z")

</div>

> [@Mitch](#):
>
> `maxvaluesvector = push!(maxvaluesvector, maxvalues)`

This is probably the issue. `push!` is pushing the _same_ vector to `maxvaluesvector` every time, so every time you edit it you’re editing all the entries simultaneously. Here’s a simple example:

```julia
julia> results = []
Any[]

julia> x = [0]
1-element Vector{Int64}:
 0

julia> for i in 1:3
         x[1] = i
         push!(results, x) # Pushing the *same* vector every time
       end

julia> results
3-element Vector{Any}:
 [3]
 [3]
 [3]

```

If you want your results to be different vectors, then you need to make that explicit. One easy way in this case is to `copy` when you `push!`:

```julia
julia> results = []
Any[]

julia> for i in 1:3
         x[1] = i
         push!(results, copy(x))
       end

julia> results
3-element Vector{Any}:
 [1]
 [2]
 [3]

```

Edit: Yup, what @sijo said 🙂

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mitch/32/24224_2.png) [@Mitch](https://discourse.julialang.org/u/Mitch)\
**Post date:** [November 17, 2021, 1:08am UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/4 "2021-11-17T01:08:06Z")

</div>

> [@rdeits](#):
>
> `push!(results, copy(x))`

Thanks @sijo, and @rdeits. I understand overwriting the initial vector is the expected behaviour and I expected that too. I didn’t expect it wouldn’t append the vector for each iteration of the outer loop, which could be thought of as each total inner loop.

I thought nested for loops followed logic like:

1. Iterate over each inner loop value [i] and store the results e.g. in a vector for the first outer loop value [j]
2. push! would store that first total inner loop given the first outer loop value [j]
3. The outer loop value [j] changes to [j] + 1 for example and the inner loop repeats and overwrites the initial storage (i.e. the first vector).
4. Push! then appends the overwritten vector to the outer loop vector, which becomes a vector of vectors.

It’s clear the logic is false, but can you help me understand where? If my thoughts aren’t clear above, just let me know how I can clarify!

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 17, 2021, 2:11am UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/5 "2021-11-17T02:11:46Z")

</div>

> [@Mitch](#):
>
> Push! then appends the overwritten vector to the outer loop vector, which becomes a vector of vectors.

The problems is: the overwritten vector that you are saving is always the same object, not a new object. You have two solutions:

1. Make the line `maxvalues = zeros(length(years))` the first line of the outer loop, so a new object is created each time.
2. Change the last line of the outer loop to `push!(maxvaluesvector, copy(maxvalues))` so you save a copy of the vector.

If you do not do either, what happens is that all positions of `maxvaluesvector` refer to the same object, that is being changed until the last iteration. You can check this by making a new change (setting the first element to zero for example) to one of the vectors inside `maxvaluesvector` and see that this change is reflected in all inner vectors instead of just that position.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 17, 2021, 8:08am UTC](https://discourse.julialang.org/t/how-to-store-data-from-a-nested-for-loop/71602/6 "2021-11-17T08:08:25Z")

</div>

Maybe it helps to look at a simpler case:

```julia
julia> v = [1,2];

julia> v_vector = [v, v, v]
3-element Vector{Vector{Int64}}:
 [1, 2]
 [1, 2]
 [1, 2]

julia> v[1] = 4;

julia> v_vector
3-element Vector{Vector{Int64}}:
 [4, 2]
 [4, 2]
 [4, 2]

```

Here the line `v_vector = [v, v, v]` is equivalent to

```julia
v_vector = typeof(v)[] # Empty vector of values with type like `v`
push!(v_vector, v)
push!(v_vector, v)
push!(v_vector, v)

```

In both cases `v_vector` contains three references to the same object.
