# How to use an anonymous function with juliacall in python

**URL:** https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132
**Category:** New to Julia
**Created:** [July 3, 2023, 3:04pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132 "2023-07-03T15:04:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![vrige\_me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vrige_me/32/50890_2.png) [@vrige\_me](https://discourse.julialang.org/u/vrige_me)
#### Post date: [July 3, 2023, 3:04pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/1 "2023-07-03T15:04:47Z")

</div>

I am new to julia and juliacall and i would like to use the following code in julia in python exploiting juliacall:

```julia
sort(collect(countmap(x)), by = tuple -> last(tuple), rev=true)

```

In my code i didn’t know how to convert the anonymous function (`tuple -> last(tuple)`). `x` is the type `Vector{DitStr{2, 8, Int64}}`. My attempt was:

```julia
from juliacall import Main as jl

jl.sort(jl.collect(jl.countmap(x)), by= jl.last(tuple), rev=true)

```

but i got this error:

```julia
Traceback (most recent call last):
  File "/home/path/PycharmProjects/path/exp1.py", line 156, in <module>
    start = time()
  File "/home/path/PycharmProjects/path/exp1.py", line 84, in check_IS
    return convert_DitStr_to_python(x, b)
  File "/home/path/PycharmProjects/path/exp1.py", line 102, in convert_DitStr_to_python
    top = jl.sort(jl.collect(jl.countmap(x)), by= jl.last(tuple), rev=true)
  File "/home/path/.julia/packages/PythonCall/1f5yE/src/jlwrap/any.jl", line 208, in __call__
    return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
AttributeError: type object 'tuple' has no attribute 'n'

```

This is the framework, but i would like to know how to use an anonymous function with juliacall

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 3, 2023, 3:06pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/2 "2023-07-03T15:06:47Z")

</div>

Did you try the standard Python lambda?

---

<div class="post-metadata">

### Author: ![vrige\_me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vrige_me/32/50890_2.png) [@vrige\_me](https://discourse.julialang.org/u/vrige_me)
#### Post date: [July 3, 2023, 3:09pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/3 "2023-07-03T15:09:04Z")

</div>

Yes, I did. But unfortunately it gave me back an error about “True” because it didn’t match “true”.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 3, 2023, 3:19pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/4 "2023-07-03T15:19:19Z")

</div>

What if you are using [`juliacall.convert`](https://cjdoris.github.io/PythonCall.jl/stable/juliacall-reference/#juliacall.convert) as the last call of the lambda?

---

<div class="post-metadata">

### Author: ![vrige\_me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vrige_me/32/50890_2.png) [@vrige\_me](https://discourse.julialang.org/u/vrige_me)
#### Post date: [July 3, 2023, 3:27pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/5 "2023-07-03T15:27:02Z")

</div>

I didn’t try with juliacall.convert because i am not really familiar with it. How could i use it?

---

<div class="post-metadata">

### Author: ![vrige\_me](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vrige_me/32/50890_2.png) [@vrige\_me](https://discourse.julialang.org/u/vrige_me)
#### Post date: [July 3, 2023, 3:41pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/6 "2023-07-03T15:41:32Z")

</div>

I have found this workaround:

> jl.sort(jl.collect(jl.zip(jl.values(jl.countmap(x)), jl.keys(jl.countmap(x)))), rev=True)

I noticed that the previous error was on “True” was my typo, so maybe it can work the standard lambda function. Thanks for the help

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [July 3, 2023, 3:50pm UTC](https://discourse.julialang.org/t/how-to-use-an-anonymous-function-with-juliacall-in-python/101132/7 "2023-07-03T15:50:56Z")

</div>

```python
jl.convert(jl.Bool, something that generates True)

```

However, after looking at the [conversion rules](https://cjdoris.github.io/PythonCall.jl/stable/conversion-to-julia/), I don’t think this explicit call should be needed.

Anyway - I noticed your comment above regarding the typo - and your workaround. Happy that it works.
