> ## Documentation Index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# LPOS

> Find the position of an element.

Use `LPOS` to find the position of an element in a list.

The scan starts at the head and, by default, reports the index of the first match or null when there is none. `RANK` selects which match to report: `RANK 2` skips to the second occurrence, and a negative rank searches backwards from the tail, so `RANK -1` finds the last occurrence. `COUNT` returns that many matching indexes instead of just one, and `COUNT 0` returns all of them. `MAXLEN` limits how many elements are compared, which bounds the cost of the search on a long list at the price of possibly missing matches beyond that point.

It is the read-only way to locate a value before acting on it with [`LSET`](/docs/redis/commands/list/lset) or [`LREM`](/docs/redis/commands/list/lrem).

## Syntax

```redis theme={"system"}
LPOS <key> <element> [RANK <rank>] [COUNT <num-matches>] [MAXLEN <len>]
```

## Arguments

| Argument              | Required | Repeatable | Description                                                  |
| --------------------- | -------- | ---------- | ------------------------------------------------------------ |
| `<key>`               | Yes      | No         | Redis key targeted by the command.                           |
| `<element>`           | Yes      | No         | Element value to search for.                                 |
| `RANK <rank>`         | No       | No         | Which match to return; negative values search from the tail. |
| `COUNT <num-matches>` | No       | No         | Number of matches to return; `0` returns every match.        |
| `MAXLEN <len>`        | No       | No         | Maximum number of entries to keep in the stream.             |

## Response

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

| Protocol | Reply                                                                  |
| -------- | ---------------------------------------------------------------------- |
| RESP2    | Null bulk string or null array, Integer, or array of integer positions |
| RESP3    | Null, Integer, or array of integer positions                           |

<Note>
  Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
</Note>

## Examples

TCP examples use the TLS `REDIS_URL` from the Upstash console. REST examples use `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN`.

<AccordionGroup>
  <Accordion title="Redis CLI" icon="terminal">
    ```bash theme={"system"}
    LPOS my-key element
    ```
  </Accordion>

  <Accordion title="@upstash/redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { Redis } from "@upstash/redis";

    const redis = Redis.fromEnv();

    await redis.rpush("key", "a", "b", "c");
    const index = await redis.lpos("key", "b");
    console.log(index); // 1
    ```
  </Accordion>

  <Accordion title="upstash_redis" icon="python" iconType="brands">
    ```python theme={"system"}
    from upstash_redis import Redis

    redis = Redis.from_env()
    result = redis.lpos("my-key", "element")
    print(result)
    ```
  </Accordion>

  <Accordion title="ioredis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import Redis from "ioredis";

    const redis = new Redis(process.env.REDIS_URL!);
    const result = await redis.lpos("my-key", "element");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="node-redis" icon="node-js" iconType="brands">
    ```ts theme={"system"}
    import { createClient } from "redis";

    const client = await createClient({ url: process.env.REDIS_URL })
      .on("error", console.error)
      .connect();
    const result = await client.lPos("my-key", "element");
    console.log(result);
    ```
  </Accordion>

  <Accordion title="redis-py" icon="python" iconType="brands">
    ```python theme={"system"}
    import os
    import redis

    client = redis.from_url(os.environ["REDIS_URL"])
    result = client.lpos("my-key", "element")
    print(result)
    ```
  </Accordion>

  <Accordion title="go-redis" icon="golang" iconType="brands">
    ```go theme={"system"}
    package main

    import (
        "context"
        "fmt"
        "os"

        "github.com/redis/go-redis/v9"
    )

    func main() {
        opts, err := redis.ParseURL(os.Getenv("REDIS_URL"))
        if err != nil {
            panic(err)
        }
        client := redis.NewClient(opts)
        result, err := client.LPos(context.Background(), "my-key", "element", redis.LPosArgs{}).Result()
        if err != nil {
            panic(err)
        }
        fmt.Println(result)
    }
    ```
  </Accordion>

  <Accordion title="jedis" icon="java" iconType="brands">
    ```java theme={"system"}
    import java.net.URI;

    import redis.clients.jedis.Jedis;

    try (Jedis jedis = new Jedis(new URI(System.getenv("REDIS_URL")))) {
      Object result = jedis.lpos("my-key", "element");
      System.out.println(result);
    }
    ```
  </Accordion>

  <Accordion title="redis-rs" icon="rust" iconType="brands">
    ```rust theme={"system"}
    use redis::{LposOptions, TypedCommands};

    fn main() -> redis::RedisResult<()> {
        let url = std::env::var("REDIS_URL").expect("REDIS_URL is not set");
        let client = redis::Client::open(url)?;
        let mut connection = client.get_connection()?;

        let result: Option<isize> = connection.lpos("my-key", "element", LposOptions::default())?;
        println!("{result:?}");
        Ok(())
    }
    ```
  </Accordion>
</AccordionGroup>


## Related topics

- [LPOS](/docs/redis/sdks/ts/commands/list/lpos.md)
- [LPOP](/docs/redis/commands/list/lpop.md)
