# How to pass values to Python?

**URL:** <https://community.wolfram.com/t/how-to-pass-values-to-python/21740>\
**Category:** Questions\
**Tags:** external-interfaces\
**Created:** [March 13, 2023, 2:26am UTC](https://community.wolfram.com/t/how-to-pass-values-to-python/21740 "2023-03-13T02:26:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oota](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/oota/32/11871_2.png) [@oota](https://community.wolfram.com/u/oota)\
**Post date:** [March 13, 2023, 2:26am UTC](https://community.wolfram.com/t/how-to-pass-values-to-python/21740/1 "2023-03-13T02:26:47Z")

</div>

I would like to pass Mathematica variable values to Python. But I found that it was not that trivial. For example, a typical way would be:

```
session = StartExternalSession["Python"];
l = Range[10];
res = ExternalEvaluate[session, "
l=list(<*l*>)
print(l)
l
"];
DeleteObject[session];

```

The results are: a Python expression [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

```
res
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

```

It seems to work. However, when I tried to define a Mathematica function with local variables:

```
f[l_] := Module[
  {session, res},
  session = StartExternalSession["Python"];
  res = ExternalEvaluate[session, "
l=list(<*l*>)
print(l)
l
"];
  DeleteObject[session];
  Return[res]
  ]

Clear[l]
f[Range[10]]

```

it didn’t work. It looks like the “\<\* \*\>” notation doesn’t accept local variables (in this case, l\_List). I tried several patterns, but all my attempts failed in vain. After all, I needed to override the local variable l\_LIst with another global variable l\_List as follows.

```
l = Range[10];
f[Range[10]]

```

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

```
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

```

Did I miss something? Any suggestion would be welcome.

[How to pass values to Python.nb](https://community.wolfram.com/uploads/short-url/iURPXFLTaWDgkYE6ZRI7SQV5pPX.nb) (73.8 KB)
