LINSERT(3valkey)
π LINSERT β the glow-up your list didn't know it needed
okay bestie, so you know how sometimes you've got a list and you're like "this element needs to go RIGHT THERE, next to that specific other element, not at the end like some basic RPUSH"? yeah. that's this. welcome to the LINSERT era. this page is from the valkey command manual, and it's about to be your new fave. π
β¨ SYNOPSIS β¨
LINSERT key <BEFORE | AFTER> pivot element
that's it. that's the whole vibe. π¦
πΈ DESCRIPTION πΈ
LINSERT slides element into the list stored at key, and it goes either before or after the reference value pivot. you pick the side. you're in control. main character energy fr.
now the little plot twists:
- if
keydoes not exist? it's just treated as an empty list, and literally nothing happens. no insert, no drama, no list created. π - if
keyexists but it does not hold a list value? an error gets returned. that's a hard no.
that's it. that's the whole ruleset.
π«Ά REPLY π«Ά
you get one of these, and they're all integer replies in valkey-protocol(7) flavor:
valkey-protocol(7)Integer reply β the list length after a successful insert operation. slay, it worked. β¨valkey-protocol(7)Integer reply β0when the key doesn't exist. (empty list, nothing happened, we move.)valkey-protocol(7)Integer reply β-1when the pivot wasn't found. the pivot ghosted you. π
β‘ COMPLEXITY β‘
O(N) where N is the number of elements to traverse before seeing the value pivot.
so real talk, translate that into vibes:
- inserting somewhere on the left end of the list (the head) can be considered
O(1)β instant, free, iconic π - inserting somewhere on the right end (the tail) is
O(N)β you're walking the whole list, bestie, pack a snack π
π ACL CATEGORIES π
@list @slow @write
π HISTORY π
- Available since: 2.2.0
(this one's been around. a legend. a classic. π )
π EXAMPLES π
127.0.0.1:6379> RPUSH mylist "Hello"
(integer) 1
127.0.0.1:6379> RPUSH mylist "World"
(integer) 2
127.0.0.1:6379> LINSERT mylist BEFORE "World" "There"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "Hello"
2) "There"
3) "World"
see how "There" just manifested between "Hello" and "World"? yeah. that's the power. BEFORE "World" said "put me right in front of the World, thank you." and it ATE. π₯
π SEE ALSO π
blmove(3valkey)blmpop(3valkey)blpop(3valkey)brpop(3valkey)brpoplpush(3valkey)lindex(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)
okay that's the whole page, go forth and insert things exactly where you want them. mwah. π
this has been version 9.0.0, dated 2025-10-21 β xoxo, your friendly neighborhood LINSERT(3valkey) π§β¨
π¦ 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