LRANGE

LRANGE key start stop
Available since:
Redis Open Source 1.0.0
Time complexity:
O(S+N) where S is the distance of start offset from HEAD for small lists, from nearest end (HEAD or TAIL) for large lists; and N is the number of elements in the specified range.
ACL categories:
@read, @list, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility

Returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.

These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.

Required arguments

key

The name of the key that holds the list.

start

The zero-based start index. Negative indexes count from the tail.

stop

The zero-based stop index (inclusive). Negative indexes count from the tail.

Examples

Foundational: Retrieve a range of elements from a list using LRANGE with start and stop indexes (supports negative indexes, inclusive range)
redis> RPUSH mylist "one"
(integer) 1
redis> RPUSH mylist "two"
(integer) 2
redis> RPUSH mylist "three"
(integer) 3
redis> LRANGE mylist 0 0
1) "one"
redis> LRANGE mylist -3 2
1) "one"
2) "two"
3) "three"
redis> LRANGE mylist -100 100
1) "one"
2) "two"
3) "three"
redis> LRANGE mylist 5 10
(empty array)
res4 = r.rpush("mylist", "one");
print(res4) # >>> 1

res5 = r.rpush("mylist", "two")
print(res5) # >>> 2

res6 = r.rpush("mylist", "three")
print(res6) # >>> 3

res7 = r.lrange('mylist', 0, 0)
print(res7) # >>> [ 'one' ]

res8 = r.lrange('mylist', -3, 2)
print(res8) # >>> [ 'one', 'two', 'three' ]

res9 = r.lrange('mylist', -100, 100)
print(res9) # >>> [ 'one', 'two', 'three' ]

res10 = r.lrange('mylist', 5, 10)
print(res10) # >>> []

const res4 = await client.rPush('mylist', 'one');
console.log(res4); // 1

const res5 = await client.rPush('mylist', 'two');
console.log(res5); // 2

const res6 = await client.rPush('mylist', 'three');
console.log(res6); // 3

const res7 = await client.lRange('mylist', 0, 0);
console.log(res7); // [ 'one' ]

const res8 = await client.lRange('mylist', -3, 2);
console.log(res8); // [ 'one', 'two', 'three' ]

const res9 = await client.lRange('mylist', -100, 100);
console.log(res9); // [ 'one', 'two', 'three' ]

const res10 = await client.lRange('mylist', 5, 10);
console.log(res10); // []

        long lRangeResult1 = jedis.rpush("mylist", "one", "two", "three");
        System.out.println(lRangeResult1); // >>> 3

        List<String> lRangeResult2 = jedis.lrange("mylist", 0, 0);
        System.out.println(lRangeResult2); // >>> [one]

        List<String> lRangeResult3 = jedis.lrange("mylist", -3, 2);
        System.out.println(lRangeResult3); // >>> [one, two, three]

        List<String> lRangeResult4 = jedis.lrange("mylist", -100, 100);
        System.out.println(lRangeResult4); // >>> [one, two, three]

        List<String> lRangeResult5 = jedis.lrange("mylist", 5, 10);
        System.out.println(lRangeResult5); // >>> []
            CompletableFuture<Void> lrange = asyncCommands.rpush("mylist", "one").thenCompose(res4 -> {
                System.out.println(res4); // >>> 1

                return asyncCommands.rpush("mylist", "two");
            }).thenCompose(res5 -> {
                System.out.println(res5); // >>> 2

                return asyncCommands.rpush("mylist", "three");
            }).thenCompose(res6 -> {
                System.out.println(res6); // >>> 3

                return asyncCommands.lrange("mylist", 0, 0);
            }).thenCompose(res7 -> {
                System.out.println(res7); // >>> [one]

                return asyncCommands.lrange("mylist", -3, 2);
            }).thenCompose(res8 -> {
                System.out.println(res8); // >>> [one, two, three]

                return asyncCommands.lrange("mylist", -100, 100);
            }).thenCompose(res9 -> {
                System.out.println(res9); // >>> [one, two, three]

                return asyncCommands.lrange("mylist", 5, 10);
            })
                    .thenAccept(res10 -> System.out.println(res10)) // >>> []
                    .toCompletableFuture();
            Mono<Void> lrange = reactiveCommands.rpush("mylist", "one").doOnNext(res4 -> {
                System.out.println(res4); // >>> 1
            }).flatMap(res4 -> reactiveCommands.rpush("mylist", "two")).doOnNext(res5 -> {
                System.out.println(res5); // >>> 2
            }).flatMap(res5 -> reactiveCommands.rpush("mylist", "three")).doOnNext(res6 -> {
                System.out.println(res6); // >>> 3
            }).flatMap(res6 -> reactiveCommands.lrange("mylist", 0, 0).collectList()).doOnNext(res7 -> {
                System.out.println(res7); // >>> [one]
            }).flatMap(res7 -> reactiveCommands.lrange("mylist", -3, 2).collectList()).doOnNext(res8 -> {
                System.out.println(res8); // >>> [one, two, three]
            }).flatMap(res8 -> reactiveCommands.lrange("mylist", -100, 100).collectList()).doOnNext(res9 -> {
                System.out.println(res9); // >>> [one, two, three]
            }).flatMap(res9 -> reactiveCommands.lrange("mylist", 5, 10).collectList()).doOnNext(res10 -> {
                System.out.println(res10); // >>> []
            }).then();
	RPushResult, err := rdb.RPush(ctx, "mylist",
		"one", "two", "three",
	).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(RPushResult) // >>> 3

	lRangeResult1, err := rdb.LRange(ctx, "mylist", 0, 0).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult1) // >>> [one]

	lRangeResult2, err := rdb.LRange(ctx, "mylist", -3, 2).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult2) // >>> [one two three]

	lRangeResult3, err := rdb.LRange(ctx, "mylist", -100, 100).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult3) // >>> [one two three]

	lRangeResult4, err := rdb.LRange(ctx, "mylist", 5, 10).Result()

	if err != nil {
		panic(err)
	}

	fmt.Println(lRangeResult4) // >>> []
        long lRangeResult1 = db.ListRightPush("mylist", ["one", "two", "three"]);
        Console.WriteLine(lRangeResult1); // >>> 3

        RedisValue[] lRangeResult2 = db.ListRange("mylist", 0, 0);
        Console.WriteLine($"[{string.Join(", ", lRangeResult2)}]");
        // >>> [one]

        RedisValue[] lRangeResult3 = db.ListRange("mylist", -3, 2);
        Console.WriteLine($"[{string.Join(", ", lRangeResult3)}]");
        // >>> [one, two, three]

        RedisValue[] lRangeResult4 = db.ListRange("mylist", -100, 100);
        Console.WriteLine($"[{string.Join(", ", lRangeResult4)}]");
        // >>> [one, two, three]

        RedisValue[] lRangeResult5 = db.ListRange("mylist", 5, 10);
        Console.WriteLine($"[{string.Join(", ", lRangeResult5)}]");
        // >>> []
        $res4 = $r->rpush('mylist', 'one');
        echo $res4 . PHP_EOL;
        // >>> 1

        $res5 = $r->rpush('mylist', 'two');
        echo $res5 . PHP_EOL;
        // >>> 2

        $res6 = $r->rpush('mylist', 'three');
        echo $res6 . PHP_EOL;
        // >>> 3

        $res7 = $r->lrange('mylist', 0, 0);
        echo json_encode($res7) . PHP_EOL;
        // >>> ["one"]

        $res8 = $r->lrange('mylist', -3, 2);
        echo json_encode($res8) . PHP_EOL;
        // >>> ["one","two","three"]

        $res9 = $r->lrange('mylist', -100, 100);
        echo json_encode($res9) . PHP_EOL;
        // >>> ["one","two","three"]

        $res10 = $r->lrange('mylist', 5, 10);
        echo json_encode($res10) . PHP_EOL;
        // >>> []

Give these commands a try in the interactive console:

RPUSH mylist "one" RPUSH mylist "two" RPUSH mylist "three" LRANGE mylist 0 0 LRANGE mylist -3 2 LRANGE mylist -100 100 LRANGE mylist 5 10

Details

Consistency with range functions in various programming languages

Note that if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements, that is, the rightmost item is included. This may or may not be consistent with the behavior of range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice or Python's range() function).

Out-of-range indexes

Out of range indexes will not produce an error. If start is larger than the end of the list, an empty list is returned. If stop is larger than the actual end of the list, Redis will treat it like the last element of the list.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
✅ Standard
✅ Active-Active
✅ Standard
✅ Active-Active

Return information

Array reply: a list of elements in the specified range, or an empty array if the key doesn't exist.
RATE THIS PAGE
Back to top ↑