LINDEX(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’… LINDEX β€” bestie edition (from the LINDEX(3valkey) page)

okay so. imagine ur list. imagine u wanna grab ONE lil element out of it by its number. that's literally it. that's the whole vibe. LINDEX is ur girl πŸ’–


✨ SYNOPSIS ✨

LINDEX key index

two args. key, then index. no more no less. periodt.


🌸 DESCRIPTION 🌸

so LINDEX returns the element sitting at index inside the list stored at key. and here's the thing β€” the index is zero-based, so 0 is the first element, 1 is the second, and so on forever and ever amen. counting from zero is giving very programmer of us but it's the rules, i don't make them.

but wait πŸŽ€ u can also go negative!! negative indices count from the tail of the list. so -1 is the last element, -2 is the penultimate one (yes that's a real word, it means second-to-last, i looked it up), and so forth. iconic behaviour honestly.

⚠️ real talk though: if the value at key is not a list, u get an error. no list, no element, no mercy. 😭

πŸ’Œ REPLY πŸ’Œ

RESP2 ✨

one of these two, depending on vibes:

  • valkey-protocol(7) Nil reply: this is what u get when index is out of range. nothing there bestie. empty. void. πŸ’€
  • valkey-protocol(7) Bulk string reply: the requested element. the actual goods. 🫢

RESP3 πŸ’–

same energy, slightly different packaging:


⏱️ COMPLEXITY ⏱️

O(N) where N is the number of elements to traverse to get to the element at index. so yeah, walking to the middle of a huge list costs u, that's just physics. BUT β€” this is the slay part β€” asking for the first or the last element of the list is O(1). constant time. instant. main character speed. πŸ”₯


πŸ” ACL CATEGORIES πŸ”

@list @read @slow

(so: it's a list thing, it reads, and it can be slow if u ask for a far-away index. noted.)


πŸ“œ HISTORY πŸ“œ

  • available since: 1.0.0 β€” she's been here since the beginning, a true OG πŸ‘‘

πŸ§ͺ EXAMPLES πŸ§ͺ

127.0.0.1:6379> LPUSH mylist "World"
(integer) 1
127.0.0.1:6379> LPUSH mylist "Hello"
(integer) 2
127.0.0.1:6379> LINDEX mylist 0
"Hello"
127.0.0.1:6379> LINDEX mylist -1
"World"
127.0.0.1:6379> LINDEX mylist 3
(nil)

see how 0 gave us "Hello" (the one we pushed LAST ends up FIRST, that's LPUSH for u, pushing to the head) and -1 gave us "World" (the tail, the oldest). and then 3? out of range. (nil). nothing. silence. πŸ¦‹


πŸ‘€ SEE ALSO πŸ‘€

blmove(3valkey), blmpop(3valkey), blpop(3valkey), brpop(3valkey), brpoplpush(3valkey), linsert(3valkey), llen(3valkey), lmove(3valkey), lmpop(3valkey), lpop(3valkey), lpos(3valkey), lpush(3valkey), lpushx(3valkey), lrange(3valkey), lrem(3valkey), lset(3valkey), ltrim(3valkey), rpop(3valkey), rpoplpush(3valkey), rpush(3valkey), rpushx(3valkey)

the whole list-girlie friend group fr fr πŸ«‚ go read them, iykyk.


okay that's the page!! πŸŽ€ this was version 9.0.0, dated 2025-10-21, from the LINDEX(3valkey) manual. mwah, go index something responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from