# Replace values of Dictionary subset

**URL:** https://discourse.julialang.org/t/replace-values-of-dictionary-subset/90605
**Category:** New to Julia
**Tags:** question, dictionary
**Created:** [November 21, 2022, 8:43pm UTC](https://discourse.julialang.org/t/replace-values-of-dictionary-subset/90605 "2022-11-21T20:43:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JohnS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johns/32/38331_2.png) [@JohnS](https://discourse.julialang.org/u/JohnS)
#### Post date: [November 21, 2022, 8:43pm UTC](https://discourse.julialang.org/t/replace-values-of-dictionary-subset/90605/1 "2022-11-21T20:43:55Z")

</div>

Hey, thanks for your help!

How can you update the values of a subset of a dictionary?

```julia
key = collect(0:1:10)
value = zeros(length(ks))
dict = Dict(ks .=> vs)
filter_set = collect(5:1:10)

```

I can filter to the desired values using the below approach, but how do I mutate those values in the dictionary?

```julia
filter(x -> x[1] in filter_set, dict)

```

If there’s a better way to “subset” a dictionary by its keys given an array of values, please let me know too, thanks!

---

<div class="post-metadata">

### Author: ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)
#### Post date: [November 21, 2022, 9:26pm UTC](https://discourse.julialang.org/t/replace-values-of-dictionary-subset/90605/2 "2022-11-21T21:26:58Z")

</div>

For mutating the dict.  
Here is a one-liner using `filter_set` as keys

```julia
foreach(x->setindex!(dict, 1,x), filter_set)

```

Otherwise, I don’t think there is anything wrong with a simple for-loop:

```julia
for subset_key in filter_set 
    dict[subset_key] = 1
end

```

---

<div class="post-metadata">

### Author: ![JohnS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johns/32/38331_2.png) [@JohnS](https://discourse.julialang.org/u/JohnS)
#### Post date: [November 21, 2022, 10:06pm UTC](https://discourse.julialang.org/t/replace-values-of-dictionary-subset/90605/3 "2022-11-21T22:06:27Z")

</div>

Thanks for the suggestion! Yeah, I was just hoping for something with a map function, but the for loop works!
