# List into list

**URL:** <https://discourse.julialang.org/t/list-into-list/44357>\
**Category:** New to Julia\
**Created:** [August 5, 2020, 5:25pm UTC](https://discourse.julialang.org/t/list-into-list/44357 "2020-08-05T17:25:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 5:25pm UTC](https://discourse.julialang.org/t/list-into-list/44357/1 "2020-08-05T17:25:02Z")

</div>

hi  
thanks this site help me to improve my knowledge in julia, so

```julia-auto
function pair_sum(nums, target)
#Given nums = [2, 7, 11, 15], target = 9,

d = Dict()
output = Int[]
count =0
for i in nums
    component = target - i
    res = []
    if component in keys(d)
        push!(res,component)
        push!(res,i)
        d[component] -=1
        append!(output,Tuple(res))
        count +=1
    else
        d[i] = 1
    end
end
println(count)
return output
end
nums = [2, 7, 11, 15 , 4 ,5]
target = 9
println("Total pairs " , pair_sum(nums,target))

```

this code is work fine, but the output is

> [2,7,4,5]

what i need is

> [[2,7] , [4,5]]

# list into list

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 5, 2020, 5:35pm UTC](https://discourse.julialang.org/t/list-into-list/44357/2 "2020-08-05T17:35:41Z")

</div>

> [@Fuad\_Sami](#):
>
> `output = Int[]`

Compare the type of your output to what you say you want: [[2,7] , [4,5]]

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 5:44pm UTC](https://discourse.julialang.org/t/list-into-list/44357/3 "2020-08-05T17:44:56Z")

</div>

the type for output is

```julia
Array{Int64,1}
[2, 7, 4, 5]

```

and if i remove the int from

```julia
Array{Any,1}
Any[2, 7, 4, 5]

```

but what i need is

```julia
[[2,7],[4,5]]

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 5, 2020, 5:53pm UTC](https://discourse.julialang.org/t/list-into-list/44357/4 "2020-08-05T17:53:58Z")

</div>

Yes. Your code outputs an array of Int. What does your needed output look like? Does it look like an array of Ints, or an array of something else?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [August 5, 2020, 7:01pm UTC](https://discourse.julialang.org/t/list-into-list/44357/5 "2020-08-05T19:01:42Z")

</div>

If you need pairs of 2, use a Tuple inside the Vector:  
`output = Tuple{Int,Int}[]`  
(If the length is unknown, use `Vector{Int}[]`)

Now, use  
`push!(output, (3,4))`  
to append a single item, or  
`append!(output, [(3,4),(5,6),(7,8)])`  
to append a number of items

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 7:16pm UTC](https://discourse.julialang.org/t/list-into-list/44357/6 "2020-08-05T19:16:30Z")

</div>

yes! that what i look for  
thanks a lot

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 5, 2020, 9:00pm UTC](https://discourse.julialang.org/t/list-into-list/44357/7 "2020-08-05T21:00:54Z")

</div>

Hello @Faud\_Sami, I see you are learning more and more! (Maybe translating more Python code as well 😉)  
Here are a couple of other ideas for this function:

```julia
# the original, pared down a bit
function pair_sum(nums, target)
    d = Set()
    output = NTuple{2, eltype(nums)}[]
    for x in nums
        y = target - x
        if y in d
            push!(output, (x, y))
        else
            push!(d, x)
        end
    end
    return output
end

# the same idea, more "direct"
# might want to ensure `nums` is unique for this
# or you'll have repeats
function pair_sum2(nums, target)
    N = length(nums)
    output = []
    for i in 1:N, j in (i+1):N
        if nums[i] + nums[j] == target
            push!(output, (nums[i], nums[j]))
        end
    end
    output
end

# if you don't care about repeats like (2, 7) and (7, 2)
pair_sum3(nums, y) = [(x, y-x) for x in nums if y-x in nums]

```

And their benchmarks

```julia-repl
julia> nums = unique(rand(1:500, 100));

julia> target = rand(1:600)
579

julia> @btime pair_sum($nums, Ref(target)[])
  10.951 μs (18 allocations: 4.50 KiB)
10-element Array{Tuple{Int64,Int64},1}:
 (331, 248)
 (429, 150)
 (152, 427)
 (161, 418)
 (300, 279)
 (426, 153)
 (334, 245)
 (277, 302)
 (85, 494)
 (188, 391)

julia> @btime pair_sum2($nums, Ref(target)[]);
  5.203 μs (16 allocations: 704 bytes)

julia> @btime pair_sum3($nums, Ref(target)[]);
  5.134 μs (11 allocations: 1.30 KiB)

```

---

<div class="post-metadata">

**Author:** ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)\
**Post date:** [August 5, 2020, 9:11pm UTC](https://discourse.julialang.org/t/list-into-list/44357/8 "2020-08-05T21:11:16Z")

</div>

amazing !  
this help 🙂

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [August 6, 2020, 3:42am UTC](https://discourse.julialang.org/t/list-into-list/44357/9 "2020-08-06T03:42:58Z")

</div>

```julia
function pair_sum(nums, target)
    filter(pair -> sum(pair) == target, collect(Iterators.partition(nums, 2)))
end

@info pair_sum([2, 7, 11, 15, 4, 5], 9) == [[2, 7], [4, 5]]

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 6, 2020, 11:05am UTC](https://discourse.julialang.org/t/list-into-list/44357/10 "2020-08-06T11:05:54Z")

</div>

> ```julia
> filter(pair -> sum(pair) == target, collect(Iterators.partition(nums, 2)))
> 
> ```

Note this only works if the pairs are consecutive and start on an odd index. (1=\>2), (5=\>6), which I think should be considered an edge case, not the general expected one.

```julia-repl
julia> pair_sum([2, 7, 11, 15, 4, 5], 9)
2-element Array{SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true},1}:
 [2, 7]
 [4, 5]

julia> pair_sum([2, 7, 11, 15, 8, 4, 5], 9)
1-element Array{SubArray{Int64,1,Array{Int64,1},Tuple{UnitRange{Int64}},true},1}:
 [2, 7]

```

---

<div class="post-metadata">

**Author:** ![wookyoung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wookyoung/32/157_2.png) [@wookyoung](https://discourse.julialang.org/u/wookyoung)\
**Post date:** [August 6, 2020, 11:48am UTC](https://discourse.julialang.org/t/list-into-list/44357/11 "2020-08-06T11:48:12Z")

</div>

in that case, needs [https://github.com/JuliaCollections/IterTools.jl](https://github.com/JuliaCollections/IterTools.jl)

```julia
using IterTools
function pair_sum(nums, target)
    filter(pair -> sum(pair) == target, collect.(IterTools.partition(nums, 2, 1)))
end

@info pair_sum([2, 7, 11, 15, 4, 5], 9) == [[2, 7], [4, 5]]
@info pair_sum([2, 7, 11, 15, 8, 4, 5], 9) == [[2, 7], [4, 5]]

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 6, 2020, 12:05pm UTC](https://discourse.julialang.org/t/list-into-list/44357/12 "2020-08-06T12:05:04Z")

</div>

> [@wookyoung](#):
>
> ```julia
> function pair_sum(nums, target)
> filter(pair -> sum(pair) == target, collect.(IterTools.partition(nums, 2, 1)))
> end
> 
> ```

The pairs still have to be consecutive.

```julia
julia> pair_sum([2, 7, 11, 15, 4, 5], 9)
2-element Array{Array{Int64,1},1}:
 [2, 7]
 [4, 5]

julia> pair_sum([2, 7, 11, 15, 4, 100, 5], 9)
1-element Array{Array{Int64,1},1}:
 [2, 7]

```

It’s also much slower 😳

```julia
julia> @btime pair_sum($nums, Ref(target)[]);
  49.949 μs (708 allocations: 47.92 KiB)

julia> @btime pair_sum2($nums, Ref(target)[]);
  7.510 μs (86 allocations: 4.66 KiB)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 6, 2020, 12:58pm UTC](https://discourse.julialang.org/t/list-into-list/44357/13 "2020-08-06T12:58:55Z")

</div>

> [@tomerarnon](#):
>
> `output = []`

This will cause the function to return a vector of `Any`:

```julia
julia> pair_sum2(nums, target)
2-element Array{Any,1}:
 (2, 7)
 (4, 5)

```

which is probably not what you want, and which has a performance penalty (though it is surprisingly small in this particular case.)

You should be wary of assignments that look like `x = []`, they signal performance issues. You could for example write

```julia
output = NTuple{2,eltype(nums)}[]

```

instead.
