LPUSH(3valkey)

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

πŸ’– LPUSH β€” bestie edition πŸ’–

okay so real quick, this is lpush from the Valkey Command Manual (section 3valkey), and it's genuinely one of my faves, no cap.

✨ NAME ✨

lpush β€” prepends one or more elements to a list. Creates the key if it doesn't exist. 🫢


πŸŽ€ SYNOPSIS πŸŽ€

LPUSH key element [element...]

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe: LPUSH inserts all the specified values at the head of the list stored at key. If key doesn't exist yet, it gets created as an empty list before the push operations even happen. πŸ’…

but β€” and this is the important part β€” if key holds a value that is not a list, an error is returned. it's giving "wrong vibe, go away." πŸ™ˆ

you can push multiple elements in a single command call just by specifying multiple arguments at the end of the command. iconic. Elements are inserted one after the other to the head of the list, going from the leftmost element to the rightmost element. So, for instance, the command LPUSH mylist a b c will result in a list containing c as first element, b as second element, and a as third element. yes bestie, the order flips, that's literally the whole point. 😭


🌸 REPLY 🌸

valkey-protocol(7) Integer reply: the length of the list after the push operation. that's the number of things in your list now, periodt. ✨


πŸ’œ COMPLEXITY πŸ’œ

O(1) for each element added, so O(N) to add N elements when the command is called with multiple arguments. fast fast fast. πŸ”₯


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@fast @list @write


🌈 HISTORY 🌈

  • Available since: 1.0.0 β€” a true OG. ⭐
  • Changed in 2.4.0: Accepts multiple element arguments. glow up. 🧚

πŸ“ 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> LRANGE mylist 0 -1
1) "hello"
2) "world"

see how "hello" ended up first even though "world" got pushed first?? that's the head-push behaviour working exactly as advertised. main character energy. πŸ’…


πŸ’– SEE ALSO πŸ’–

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

go read those too, they're all part of the list fam and they ate. πŸ‘‘


ngl the whole thing is giving "just read the docs and you'll be fine" and honestly? you will. 🫢

and that's a wrap bestie!! this page is from version 9.0.0, dated 2025-10-21 β€” luv u, go push some lists πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from