LINDEX(3valkey)
π
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:
- valkey-protocol(7) Null reply: when index is out of range.
- valkey-protocol(7) Bulk string reply: the requested element. π
β±οΈ 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
- 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