LINSERT(3valkey)

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

πŸ’– 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 key does not exist? it's just treated as an empty list, and literally nothing happens. no insert, no drama, no list created. πŸ’€
  • if key exists 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 β€” 0 when the key doesn't exist. (empty list, nothing happened, we move.)
  • valkey-protocol(7) Integer reply β€” -1 when 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 πŸ‘€


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