Replace values of Dictionary subset

Hey, thanks for your help!

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

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?

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!

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

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

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

for subset_key in filter_set 
    dict[subset_key] = 1
end
2 Likes

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