snowflake.snowpark.functions.array_position

snowflake.snowpark.functions.array_position(variant: Union[Column, str], array: Union[Column, str]) → Column[source]

Returns the zero-based index of the first occurrence of an element in an ARRAY.

The first element has index 0. If the value is not present, returns SQL NULL (represented by None in a collected Row), not -1.

Parameters:
  • variant – Column containing the VARIANT value that you want to find. The function searches for the first occurrence of this value in the array.

  • array – Column containing the ARRAY to be searched.

Example::
>>> from snowflake.snowpark.functions import array_position, lit
>>> df = session.create_dataframe(
...     [(1, [2, 1, 1]), (2, [1, 3]), (3, [4, 5])],
...     schema=["id", "values"])
>>> df.select("id", array_position(lit(1), "values").alias("position")).sort("id").collect()
[Row(ID=1, POSITION=1), Row(ID=2, POSITION=0), Row(ID=3, POSITION=None)]

In the first row, 1 appears twice; the result is the position of its first occurrence. Use lit() to search for a literal value rather than a column name.