# How to improve this code

**URL:** <https://discourse.julialang.org/t/how-to-improve-this-code/38535>\
**Category:** General Usage\
**Created:** [May 1, 2020, 8:48am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535 "2020-05-01T08:48:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [May 1, 2020, 8:48am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535/1 "2020-05-01T08:48:04Z")

</div>

Hi,

I have a large number of key dictionaries inside a dictionary named **data**. I would like to iterate over the collection of dicts to find the bus of type 12, which must be only one in the collection. I came up with the following piece of lines, but is there any improvement for this to save time?

```julia
for i = 1:length(data["bus"])
    if data["bus"]["$(i)"]["type"] == 12
        global new_key = data["bus"]["$(i)"]["key"];
    end
end

```

Thank you

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 1, 2020, 9:12am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535/2 "2020-05-01T09:12:01Z")

</div>

Is this a homework problem?

In a real-life application, I would consider the following first:

1. choose another datastructure for buses, eg a `struct` or a `NamedTuple`,
2. don’t convert to strings for keys, use `i` directly,
3. wrap the whole thing in a function, and just pass `data["bus"]` to it,
4. think about returning the whole bus datastructure instead of `key` (but this depends on the context)
5. use a standard idiom like `findall` or `filter` for getting buses of a given type (are they unique by type?)

See the manual for all of these.

An alternative is just dumping everything in a dataframe using a “tidy” structure. See

[https://juliadata.github.io/DataFrames.jl/stable/](https://juliadata.github.io/DataFrames.jl/stable/)

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [May 1, 2020, 9:29am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535/3 "2020-05-01T09:29:53Z")

</div>

Hi @Tamas_Papp,

No, it is not a homework, but I am just working on a project and would like to improve it.

How can `findall` work in this case?

Thank you!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 1, 2020, 9:33am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535/4 "2020-05-01T09:33:49Z")

</div>

for starter I guess, notice you’re reading `data["bus"]["$(i)"]` twice (I assume)

---

<div class="post-metadata">

**Author:** ![hmemy](https://avatars.discourse-cdn.com/v4/letter/h/50afbb/32.png) [@hmemy](https://discourse.julialang.org/u/hmemy)\
**Post date:** [May 1, 2020, 9:35am UTC](https://discourse.julialang.org/t/how-to-improve-this-code/38535/5 "2020-05-01T09:35:29Z")

</div>

yeah, the first one is for checking condition, and the other is for assigning if condition is met.
