# Use get! to get all elements in array

**URL:** <https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008>\
**Category:** New to Julia\
**Created:** [August 26, 2019, 7:35pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008 "2019-08-26T19:35:19Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 26, 2019, 7:35pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/1 "2019-08-26T19:35:19Z")

</div>

Hi, I have question about function get!. So I have this lines:

```julia
d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33])
a=["2","3","4","5"]
t=Tuple(a)

```

Can i use function “get” for my variable a and t? I mean get all elements which are in Array/Tuple and in key in my dictionary. I want to know if it’s possible do in one line. Without using any loops. Only put something in blank place below.

```julia
get!(d, , ["66"])

```

or

```julia
get(d, ,[*here i want number what we check if is in keys(d)*])

```

PS: How format text to julia syntax?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 26, 2019, 7:41pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/2 "2019-08-26T19:41:13Z")

</div>

> [@elemenopeereste](#):
>
> PS: How format text to julia syntax?

you format text with backticks, `````, one backtick is for inline code and three backticks is for a block of code

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 26, 2019, 7:49pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/3 "2019-08-26T19:49:27Z")

</div>

there are good functions for this: `intersect` and `union`

```julia
d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33])
a=["2","3","4","5"]
xx = intersect(a,keys(d)) # ["2","3"]
map(x->d[x],intersect(a,keys(d))) #one liner
#all the elements of the dictionary d that have a key in common with a

```

is good to note that is not necessary to do Tuple(a), it works with any iterable

---

<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 26, 2019, 8:07pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/4 "2019-08-26T20:07:33Z")

</div>

Here’s a faster and still quite nice alternative

```julia
[d[x] for x in a if x in keys(d)]

```

Do you also need the `["66"]` you put in one of the lines? Not sure exactly what output you want.

---

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 26, 2019, 8:16pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/5 "2019-08-26T20:16:20Z")

</div>

I want to add new element to Dict, so i write if it’s posible using get!

> Do you also need the `["66"]` you put in one of the lines? Not sure exactly what output you want.

Yes my mistake, i mean for sure [any number] best if will be this one what i want to check, mean contain in keys(d) if not, add to Dict.

---

<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 26, 2019, 8:18pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/6 "2019-08-26T20:18:43Z")

</div>

I still don’t quite get it. Could you write out the inputs and the expected output? I guess you have written down the inputs, but I still don’t know what the output should be.

---

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 26, 2019, 8:39pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/7 "2019-08-26T20:39:26Z")

</div>

```julia
#input
d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33])
a=["2","3","4","5"]
#do get!(...)
#output
Dict{String,Array{Int64,1}} with 5 entries:
  "1" => [1, 11]
  "2" => [2]
  "3" => [3, 13, 33]
  "4" => [4]
  "5" => [5]

```

I want to do the same like this, only without using loop:

```julia
for val in a
   get!(d, val, [parse(Int64, val)]
end

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 26, 2019, 8:59pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/8 "2019-08-26T20:59:44Z")

</div>

> [@elemenopeereste](#):
>
> I want to do the same like this, only without using loop:
> 
> ```julia
> for val in a
> get!(d, val, [parse(Int64, val)]
> end
> 
> ```

sincerely, that looks very good. im looking for ways to do that in one line for now

---

<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 26, 2019, 9:00pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/9 "2019-08-26T21:00:30Z")

</div>

```julia
jl> z = get!.((d,), a, [[parse(Int, x)] for x in a])
4-element Array{Array{Int64,1},1}:
 [2]        
 [3, 13, 33]
 [4]        
 [5]        

jl> d
Dict{String,Array{Int64,1}} with 5 entries:
  "4" => [4]
  "1" => [1, 11]
  "5" => [5]
  "2" => [2]
  "3" => [3, 13, 33]

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 26, 2019, 9:09pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/10 "2019-08-26T21:09:55Z")

</div>

Why do you want to get rid of the for-loop? This looks quite readable, and in Julia, there’s nothing wrong with just using a for-loop. You might be coming from Python, where native loops are quite slow, but in Julia, these don’t come at any extra cost compared to broadcasting, for example, so you are free to use whatever feels more natural.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 26, 2019, 9:10pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/11 "2019-08-26T21:10:09Z")

</div>

this only modifies the dict

```julia
foreach(val->get!(d, val, [parse(Int64, val)]),a)

```

i was reading the julia documentation 😄 ,is only one line as requested, but your loop is the most elegant solution in my opinion (and in julia, that will run pretty fast)

---

<div class="post-metadata">

**Author:** ![elemenopeereste](https://avatars.discourse-cdn.com/v4/letter/e/73ab20/32.png) [@elemenopeereste](https://discourse.julialang.org/u/elemenopeereste)\
**Post date:** [August 26, 2019, 9:20pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/12 "2019-08-26T21:20:24Z")

</div>

I reading about tuples and using it in function like do\_sth(tuple…) and i think how write this in one line. If is possible do something not only for one element like get(Dict, element, new\_value) but for more than one element like get(Dict, tuple…, new\_val)(yep i know this syntax isn’t correct).  
I coming from C++ 😃

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 26, 2019, 9:24pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/13 "2019-08-26T21:24:22Z")

</div>

> [@DNF](#):
>
> z = get!.((d,), a, [[parse(Int, x)] for x in a])

You can just omit the square brackets around the generator expression, which will also make it non-allocating.

---

<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 26, 2019, 9:29pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/14 "2019-08-26T21:29:09Z")

</div>

I did try that, but got identical runtime and allocations.

That is, I tried

```julia
get!.((d,), a, ([parse(Int, x)] for x in a))

```

I’m surprised I get a generator without the parens around the expression…

```julia
julia> @btime get!.((d,), $a, ([parse(Int, x)] for x in $a)) setup=(d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33]))

  512.271 ns (11 allocations: 688 bytes)

julia> @btime get!.((d,), $a, [parse(Int, x)] for x in $a) setup=(d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33]))
  507.953 ns (11 allocations: 688 bytes)

julia> @btime get!.((d,), $a, [[parse(Int, x)] for x in $a]) setup=(d=Dict("1"=>[1,11], "2"=>[2], "3"=>[3,13,33]))

  509.099 ns (11 allocations: 688 bytes)
  

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 26, 2019, 9:36pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/15 "2019-08-26T21:36:50Z")

</div>

You seem to be referring to argument splatting in functions. Have you read this section in the docs?

[https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions-1](https://docs.julialang.org/en/v1/manual/functions/#Varargs-Functions-1)

This syntax is very powerful in a lot of cases, but it doesn’t really seem applicable here. The purpose of splatting is just to pass the elements of an iterable to a function as different arguments, but there aren’t any methods defined for `get!` with more than three arguments, so this will just throw an error.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [August 26, 2019, 9:50pm UTC](https://discourse.julialang.org/t/use-get-to-get-all-elements-in-array/28008/16 "2019-08-26T21:50:16Z")

</div>

I’m not 100% sure what is going on here, but I don’t know how representative such a micro-benchmark is, since you’re measuring a lot of different things here. The compiler might even be able to do some fancy stuff for small collections, but I would still recommend lazy generators for most applications, since the compiler doesn’t have to allocate the whole array on the heap.
