# Python and Julia arrays indexing

**URL:** <https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792>\
**Category:** New to Julia\
**Tags:** question, pycall\
**Created:** [December 14, 2020, 7:54am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792 "2020-12-14T07:54:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![R\_Surya\_Narayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r_surya_narayan/32/7867_2.png) [@R\_Surya\_Narayan](https://discourse.julialang.org/u/R_Surya_Narayan)\
**Post date:** [December 14, 2020, 7:54am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/1 "2020-12-14T07:54:26Z")

</div>

Hello, I am trying to use a reaction solver package of Python called Cantera in Julia using PyCall. In the following code, I am trying to extract all reactions that contain “CO” and “CO2” in the reactant side and product side respectively. `gas` is a Python Object with `gas.reactions()` producing an array of Python Objects containing all the reactions. The reactants and products are defined as dictionaries and can be accessed for _each_ reaction using `.reactants` and `.products`.  
This is the code I wrote for the task at hand:

```julia
using PyCall
@pyimport cantera as ct
gas=ct.Solution("gri30.xml");
II = [i for (i,r) in enumerate(gas.reactions()) if "CO" in keys(r.reactants) && "CO2" in keys(r.products)]
for i in II
    print("\n")
    print(gas.reaction(i).equation)
end

```

Surprisingly, I get the following output:

```julia
HCO + O <=> CO + OH
CH2O + O2 <=> HCO + HO2
HCO + OH <=> CO + H2O
CH2O + HO2 <=> H2O2 + HCO

```

_However_, if I change the `i` inside the array `II` to `i-1`

```julia
II = [i-1 for (i,r) in enumerate(gas.reactions()) if "CO" in keys(r.reactants) && "CO2" in keys(r.products)]
for i in II
    print("\n")
    print(gas.reaction(i).equation)
end

```

it works perfectly well:

```julia
CO + O (+M) <=> CO2 (+M)
CO + O2 <=> CO2 + O
CO + OH <=> CO2 + H
CO + HO2 <=> CO2 + OH

```

Any insights on why such a thing happens? My initial guess was python arrays start with 0 while Julia arrays start with 1.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 14, 2020, 8:23am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/2 "2020-12-14T08:23:21Z")

</div>

> [@R\_Surya\_Narayan](#):
>
> python arrays start with 0 while Julia arrays start with 1.

Yes. That is it.  
[https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-Python)

---

<div class="post-metadata">

**Author:** ![R\_Surya\_Narayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r_surya_narayan/32/7867_2.png) [@R\_Surya\_Narayan](https://discourse.julialang.org/u/R_Surya_Narayan)\
**Post date:** [December 14, 2020, 8:39am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/3 "2020-12-14T08:39:58Z")

</div>

@lmiq thank you for the link to the documentation. However, what I felt was invoking `PyCall` automatically accounts for this difference and I needn’t correct for it each time.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 14, 2020, 8:43am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/4 "2020-12-14T08:43:38Z")

</div>

> [@R\_Surya\_Narayan](#):
>
> I felt was invoking `PyCall` automatically accounts for this difference and I needn’t correct for it each time.

It does, but you are not accessing the element with `getindex` but rather through a function `(i)` and PyCall thus does not know to shift the index by 1 in this case.

Had you accessed an array with `array[i]`, PyCall would handle it.

---

<div class="post-metadata">

**Author:** ![weiqi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weiqi/32/22148_2.png) [@weiqi](https://discourse.julialang.org/u/weiqi)\
**Post date:** [February 20, 2021, 5:06am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/5 "2021-02-20T05:06:08Z")

</div>

Just saw this discussion. I have been working on a native Julia package for combustion those days, called Arrhenius.jl at [https://github.com/DENG-MIT/Arrhenius.jl](https://github.com/DENG-MIT/Arrhenius.jl). I made the initial version within a couple of hours since it is built on top of Cantera’s mechanism interpreter! Part of the reason to make it is to avoid the pain of coupling between python and Julia … But the most important motivation is to have a native Julia code so that we can use Julia’s auto-differentiation packages.

I assume what you are looking for is the reaction source term since it is relatively simple to code governing equations. If that’s the case, definitely check out the package.

---

<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:** [February 20, 2021, 6:01am UTC](https://discourse.julialang.org/t/python-and-julia-arrays-indexing/51792/6 "2021-02-20T06:01:31Z")

</div>

```julia
julia> a = PyObject([1,2,3])
PyObject array([1, 2, 3], dtype=int64)

julia> a[0]
3

julia> a[1]
1

julia> a[2]
2

julia> a[3]
3

julia> a[-1]
2

```

I’m confused by this half-julia half-python behavior, seems to be wrong in both worlds
