# Filter dict and change its keys

**URL:** <https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873>\
**Category:** General Usage\
**Created:** [February 19, 2020, 6:51pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873 "2020-02-19T18:51:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [February 19, 2020, 6:51pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/1 "2020-02-19T18:51:43Z")

</div>

Is is possible to filter a dictionary and change its keys elegantly?

```julia
julia> dict = Dict{Tuple{Symbol,Symbol},Expr}((:X₁, :X₁) => :(σX(σ[1], X₁)), (:X₁, :X₂) => :(σX(σ[1], X₁)),(:X₂, :X₂) => :(σX(σ[2], X₂)),(:X₃, :X₃) => :(σX(σ[3], X₃)))
Dict{Tuple{Symbol,Symbol},Expr} with 4 entries:
  (:X₁, :X₁) => :(σX(σ[1], X₁))
  (:X₂, :X₂) => :(σX(σ[2], X₂))
  (:X₃, :X₃) => :(σX(σ[3], X₃))
  (:X₁, :X₂) => :(σX(σ[1], X₁))

julia> filter(p -> p.first[1] == :X₁, dict)
Dict{Tuple{Symbol,Symbol},Expr} with 2 entries:
  (:X₁, :X₁) => :(σX(σ[1], X₁))
  (:X₁, :X₂) => :(σX(σ[1], X₁))

```

I would like to obtain a dictionary with its keys changed using the `p.first[2]` as values, i.e.:

```julia
Dict{Symbol,Expr} with 2 entries:
  :X₁ => :(σX(σ[1], X₁))
  :X₂ => :(σX(σ[1], X₁))

```

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [February 19, 2020, 6:59pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/2 "2020-02-19T18:59:23Z")

</div>

I would do something like:

```julia
julia> Dict(k[2] => value for (k, value) in dict if k[1] == :X₁)
Dict{Symbol,Expr} with 2 entries:
  :X₂ => :(σX(σ[1], X₁))
  :X₁ => :(σX(σ[1], X₁))

```

With Dict I create a new Dictionary from the values of the previous one, with an if as filter.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [February 19, 2020, 7:02pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/3 "2020-02-19T19:02:13Z")

</div>

So far I am using:

```julia
Dict(p.first[2] => p.second for p in filter(p -> p.first[1] == :X₁, dict))

```

which is similar, but I think I am creating an unnecessary dict in the middle.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [February 19, 2020, 7:10pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/4 "2020-02-19T19:10:13Z")

</div>

Yes, it is similar (I am using the iterator over the dict instead instead of using a filter). In both cases, a new Dictionary is created. I think you cannot avoid it, because the first Dictionary, dict, has a different type than the desired output.

---

<div class="post-metadata">

**Author:** ![rvignolo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rvignolo/32/33498_2.png) [@rvignolo](https://discourse.julialang.org/u/rvignolo)\
**Post date:** [February 19, 2020, 7:12pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/5 "2020-02-19T19:12:18Z")

</div>

but besides the output dict, which is a new dict of different type, I think I am creating a new dict as output of the filter function. However, I am not sure because each object of this dictionary might be a reference of the input dictionary, not a copy.

Thanks!

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [February 19, 2020, 7:14pm UTC](https://discourse.julialang.org/t/filter-dict-and-change-its-keys/34873/6 "2020-02-19T19:14:19Z")

</div>

Yes, you are right. My proposal uses less memory than your, maybe because you are creating a temporal dictionary using the filter. I think I am creating also copies, but I am not sure about how to avoid it.

You’re welcome.
