LPUSH(3valkey)
π 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
- 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