# What would be an equivalent julia code

**URL:** <https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 28, 2024, 8:23am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341 "2024-06-28T08:23:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 28, 2024, 8:23am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/1 "2024-06-28T08:23:02Z")

</div>

I have a piece of code in python

```julia
def average_by(lst, fn = lambda x: x):
  return sum(map(fn, lst), 0.0) / len(lst)

average_by([{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }], lambda x: x['n'])
# 5.0

```

What would be the equivalent julia code?

```julia
function average_by(lst; fn = x -> x)
  return sum(map(fn, lst)) / length(lst)
end
 a = Dict("1" => 2,"2" => 3)
 average_by(a,fn = x -> x["n"])

```

Error : `ERROR: map is not defined on dictionaries`

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 28, 2024, 8:41am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/2 "2024-06-28T08:41:55Z")

</div>

Give a reproducible example.

---

<div class="post-metadata">

**Author:** ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)\
**Post date:** [June 28, 2024, 8:42am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/3 "2024-06-28T08:42:57Z")

</div>

It works though; Make sure `lst` is a Vector (List) of dictionaries, and not a dictionary itself (Same as the Python code):

```julia
julia> lst = [Dict("n" => 4), Dict("n" => 2), Dict("n" => 8), Dict("n" => 6)]
4-element Vector{Dict{String, Int64}}:
 Dict("n" => 4)
 Dict("n" => 2)
 Dict("n" => 8)
 Dict("n" => 6)

julia> function average_by(lst; fn = x -> x)
         return sum(map(fn, lst)) / length(lst)
       end
average_by (generic function with 1 method)

julia> average_by(lst; fn = x -> x["n"])
5.0

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 28, 2024, 9:23am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/4 "2024-06-28T09:23:33Z")

</div>

You could also try `mean(f, itr)` (available with `using Statistics`).

For example, `mean(x -> x^2, [1,2,3])`.

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 28, 2024, 9:46am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/5 "2024-06-28T09:46:46Z")

</div>

> [@ericphanson](#):
>
> mean(x → x^2, [1,2,3])

```julia

julia> a = Dict("1" => 2,"2" => 3)                                                 
Dict{String, Int64} with 2 entries:                                                 
  "1" => 2                                                                          
  "2" => 3                                                                          
                                                                                    
julia> mean(x -> x["n"], a)                                                         
ERROR: MethodError: no method matching getindex(::Pair{String, Int64}, ::String)

```

```julia
julia> st = [Dict("n" => 4), Dict("n" => 2), Dict("n" => 8), Dict("n" => 6)]       
4-element Vector{Dict{String, Int64}}:                                             
 Dict("n" => 4)                                                                    
 Dict("n" => 2)                                                                    
 Dict("n" => 8)                                                                    
 Dict("n" => 6)                                                                    
                                                                                   
julia> mean(x -> x["n"], st)                                                      
5.0

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 28, 2024, 9:51am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/6 "2024-06-28T09:51:05Z")

</div>

are those the results you expected, or is there something you want to be able to do but are having trouble?

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 28, 2024, 9:53am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/7 "2024-06-28T09:53:02Z")

</div>

I should have defined argument as list of dictionaries. Whatever you told is one of the solutions.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 28, 2024, 10:38am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/8 "2024-06-28T10:38:14Z")

</div>

> [@VinodV](#):
>
> ```julia
>                                                                      
> julia> mean(x -> x["n"], a)                                                         
> ERROR: MethodError: no method matching getindex(::Pair{String, Int64},
> 
> ```

try

```julia
average_by(values(a))

```

---

<div class="post-metadata">

**Author:** ![VinodV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinodv/32/32733_2.png) [@VinodV](https://discourse.julialang.org/u/VinodV)\
**Post date:** [June 28, 2024, 10:49am UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/9 "2024-06-28T10:49:44Z")

</div>

```julia
[quote="Qfl3x, post:3, topic:116341"]
`lst = [Dict("n" => 4), Dict("n" => 2), Dict("n" => 8), Dict("n" => 6)]`
[/quote]

julia> a = [Dict("n" => 4), Dict("n" => 2), Dict("n" => 8), Dict("n" => 6)]
4-element Vector{Dict{String, Int64}}:
 Dict("n" => 4)
 Dict("n" => 2)
 Dict("n" => 8)
 Dict("n" => 6)

julia> average_by(values(a))
ERROR: MethodError: no method matching +(::Dict{String, Int64}, ::Dict{String, Int64})

```

```julia
julia> a = Dict("1" => 2,"2" => 3)
Dict{String, Int64} with 2 entries:
  "1" => 2
  "2" => 3

julia> average_by(values(a))
2.5

```

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [June 28, 2024, 12:10pm UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/11 "2024-06-28T12:10:09Z")

</div>

Your code already works, but I would use `Iterators.map` to handle arbitrary iterables.  
Another issue is what should happen if your iterator is empty. The default implementation errors, which is probably the most reasonable thing to do. But you may want to return `NaN` instead.

---

<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:** [June 28, 2024, 6:17pm UTC](https://discourse.julialang.org/t/what-would-be-an-equivalent-julia-code/116341/12 "2024-06-28T18:17:48Z")

</div>

> [@Qfl3x](#):
>
> ```julia
> sum(map(fn, lst)) / length(lst)
> 
> ```

This will first map over the vector, creating an unnecessary intermediate collection (particularly unnecessary for the default map function). It’s better to use the `sum` function with the mapping argument, like this:

```julia
sum(fn, lst) / length(lst) 

```

> [@Qfl3x](#):
>
> `function average_by(lst; fn = x -> x)`

As for the function signature, two points

- `fn` isn’t a kwarg in the Python code, so the semicolon should be a comma.
- There’s actually a function in Base that is equal to `x -> x`, called `identity`. It doesn’t make any difference here, but it’s common to use it as a ‘default function’.
