LRANGE(3valkey)

๐Ÿ“ฆ
version 9.0.6 ยท 2 versions
, in fedora 44

๐Ÿ’– 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 start is larger than the end of the list, an empty list is returned. she's just not gonna engage with that request.
  • If stop is 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:


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