LRANGE

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Get a list range by index

Usage is lrange key start stop.
From left to right, the first value is 0, the second is 1, and the third is 2.
To get the whole list, set start to 0 and stop to -1.

Example

Command>lpush mylist value1 value2 value3 value4
Result>4
Command>lrange mylist 0 -1
Result>0) value4
1) value3
2) value2
3) value1
Command>lrange mylist 0 1
Result>0) value4
1) value3
Command>lrange mylist 2 -1   Get data from the third item to the end
Result>0) value2
1) value1

View animation


Use negative numbers from the right

From right to left, the first value is -1 and the second is -2.

Negative index example

Command>lrange mylist -2 -1
Result>0) value2
1) value1
Command>lrange mylist -4 -3
Result>0) value4
1) value3
Command>lrange mylist 0 -3   Get all except the last two
Result>0) value4
1) value3

View animation


Out-of-range indexes do not cause an error

There are 4 items, but entering 100 does not cause an error.

Example

Command>lrange key 3 100
Result>0) value1
Command>lrange key -100 -90
Result>   No result

Sort feature

SORT or ASC sorts in ascending order, DESC sorts in descending order.

Example

Command>lrange mylist 0 -1 sort
Result>1) value1
2) value2
3) value3
4) value4
Command>lrange mylist 0 -1 desc
Result>1) value4
2) value3
3) value2
4) value1
The sort feature is available on Enterprise servers.

Syntax

LRANGE key start stop [sort|asc|desc]

  • Available since version 1.0.0.
  • Time complexity is O(S+N). S is start and N is the number of elements.
SQL Select If Redis Enterprise 7.3.0 or later, try SELECT.
Related commands LPUSH, RPUSH, LPOP, RPOP
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< RPUSHS LRANGE LLS >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.