LRANGE(3valkey)
๐ LRANGE ๐
okay so bestie, welcome to LRANGE from the Valkey Command Manual โ this is the girl who hands you a slice of your list. not the whole list, just the slice you asked for. she's been on the scene since forever and she's still serving. โจ
๐ธ NAME ๐ธ
lrange โ returns a range of elements from a list. that's it. that's the whole job. and she does it flawlessly. ๐
๐ SYNOPSIS ๐
LRANGE key start stop
three little arguments, one big vibe. ๐ฆ
โจ DESCRIPTION โจ
So basically, LRANGE returns the specified elements of the list stored at key. The offsets start and stop are zero-based indexes โ meaning 0 is the first element of the list (the head of the list, main character position obviously), 1 is the next element, and so on forever and ever. ๐
These offsets can also be negative numbers, which means they start counting from the end of the list instead. For example, -1 is the last element of the list, -2 the penultimate, and so on. it's giving counting backwards while still being correct. ๐
๐ง Consistency with range functions in various programming languages
okay so this is the part where everyone gets confused, so listen up bestie. 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. Both ends are inclusive, periodt. ๐
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. so like, vibe check your language before you assume. ๐
๐ Out-of-range indexes
Out of range indexes will not produce an error. no drama, no exception, no screaming. ๐ซถ
- If
startis larger than the end of the list, an empty list is returned. she's just not gonna engage with that request. - If
stopis larger than the actual end of the list, Valkey will treat it like the last element of the list. generous queen behavior honestly.
๐ REPLY ๐
valkey-protocol(7) Array reply: a list of elements in the specified range, or an empty array if the key doesn't exist. ๐
โก 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. math but make it cute. ๐งฎโจ
๐ ACL CATEGORIES ๐
@list@read@slow
๐ HISTORY ๐
- Available since:
1.0.0โ she's an OG fr fr. ๐
๐ EXAMPLES ๐
watch her work, bestie:
127.0.0.1:6379> RPUSH mylist "one"
(integer) 1
127.0.0.1:6379> RPUSH mylist "two"
(integer) 2
127.0.0.1:6379> RPUSH mylist "three"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 0
1) "one"
127.0.0.1:6379> LRANGE mylist -3 2
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> LRANGE mylist -100 100
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> LRANGE mylist 5 10
(empty array)
see how LRANGE mylist 0 0 gave exactly one element? inclusive both ends, always. and LRANGE mylist 5 10 gave the empty array with zero drama. ate. ๐
๐ SEE ALSO ๐
the whole friend group, go say hi:
blmove(3valkey)blmpop(3valkey)blpop(3valkey)brpop(3valkey)brpoplpush(3valkey)lindex(3valkey)linsert(3valkey)llen(3valkey)lmove(3valkey)lmpop(3valkey)lpop(3valkey)lpos(3valkey)lpush(3valkey)lpushx(3valkey)lrem(3valkey)lset(3valkey)ltrim(3valkey)rpop(3valkey)rpoplpush(3valkey)rpush(3valkey)rpushx(3valkey)
reminder bestie: indexes are zero-based, both ends are inclusive, negative indexes count from the back, and out-of-range will never yell at you. no cap. ๐ซถ
version 9.0.0 ยท dated 2025-10-21 โ that's the tea, that's the whole page, mwah ๐ go slay your lists, ily, bye!! ๐ธ๐ฆโจ
๐ฆ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 ยท CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 ยท CC-BY-SA-4.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms