Back to Redis

LPUSH

content/commands/lpush.md

latest1.9 KB
Original Source

Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.

It is possible to push multiple elements using a single command call just specifying multiple arguments at the end of the command. Elements are inserted one after the other to the head of the list, from the leftmost element to the rightmost element. So for instance the command LPUSH mylist a b c will result into a list containing c as first element, b as second element and a as third element.

Examples

{{< clients-example set="cmds_list" step="lpush" description="Foundational: Add one or more elements to the head of a list using LPUSH (creates list if needed, returns new list length)" difficulty="beginner" >}} redis> LPUSH mylist "world" (integer) 1 redis> LPUSH mylist "hello" (integer) 2 redis> LRANGE mylist 0 -1

  1. "hello"
  2. "world" {{< /clients-example >}}

Give these commands a try in the interactive console:

{{% redis-cli %}} LPUSH mylist "world" LPUSH mylist "hello" LRANGE mylist 0 -1 {{% /redis-cli %}}

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |

Return information

{{< multitabs id="lpush-return-info" tab1="RESP2" tab2="RESP3" >}}

Integer reply: the length of the list after the push operation.

-tab-sep-

Integer reply: the length of the list after the push operation.

{{< /multitabs >}}