List retrieval and pagination
Read all values in a list with 0 -1 or fetch a bounded slice for display.
Redis · Command
Returns an inclusive range of elements from a Redis list. Start and stop use zero-based indexes, and negative indexes count from the list's tail.
LRANGE reads the elements between the supplied zero-based start and stop positions from a Redis list. Both boundaries are inclusive, negative positions count backward from the tail, and indexes outside the list are tolerated rather than treated as errors.
LRANGE key start stop| Name | Type | Description |
|---|---|---|
key | key | Name of the key that holds the list. |
start | integer | Zero-based starting index; negative indexes count from the tail. |
stop | integer | Inclusive zero-based ending index; negative indexes count from the tail. |
An array containing the selected list elements, or an empty array if the key does not exist.
redis-cli RPUSH mylist one two three
redis-cli LRANGE mylist 0 -1
# 1) "one"
# 2) "two"
# 3) "three"Adds three values, then reads the entire list with an inclusive range from the first element through the last.
Read all values in a list with 0 -1 or fetch a bounded slice for display.
Inspect the newest entries by using a negative range such as -5 -1.
Read a queue segment without removing its jobs.
LRANGE returns a selected portion of a list using zero-based start and stop indexes. The stop index is inclusive, so 0 0 reads the first element and 0 -1 reads the whole list.
Follow-up: How does it behave when indexes exceed the list? Out-of-range indexes do not cause errors; the result contains the overlapping elements, or is empty when the range does not overlap.
Negative indexes count from the tail: -1 identifies the last element, -2 the previous one, and so forth. This makes 0 -1 a convenient full-list read.
Follow-up: What happens if the key is absent? Redis returns an empty array.
The command has O(S+N) complexity, where S is the distance from the relevant list end to the start offset and N is the number of returned elements. Reading a large range therefore costs more than reading a small one.
Follow-up: Which list operation can prepare the example list? RPUSH appends values to the list, after which LRANGE can read them.
RPUSHLPOPLLEN