Documentation
List Commands

List commands

These are the commands that are available for lists.

LPUSH <key> <value>

Push a value to the left of a list. Create a list if it doesn't exist.

Example

> LPUSH list 1
1

LPUSHX <key> <value>

Push a value to the left of a list only if the list exists.

Example

# List doesn't exist
> LPUSHX list 1
 
# List exists
> LPUSH list 1
1
 
> LPUSHX list 2
2

RPUSH <key> <value>

Push a value to the right of a list. Create a list if it doesn't exist.

Example

> RPUSH list 2
2

RPUSHX <key> <value>

Push a value to the right of a list only if the list exists.

Example

# List doesn't exist
> RPUSHX list 1
 
# List exists
> RPUSH list 1
1
 
> RPUSHX list 2
2

LPOP <key>

Pop a value from the left of a list. Return nothing if the list is empty.

Example

> LPOP list
1
 
> LPOP list
2
 
# Empty list
> LPOP list

RPOP <key>

Pop a value from the right of a list. Return nothing if the list is empty.

Example

> RPOP list
2
 
> RPOP list
1
 
# Empty list
> RPOP list

LLEN <key>

Get the length of a list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LLEN list
2

LTRIM <key> <start> <stop>

Trim a list to the specified range.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LTRIM list 0 1
ok

LINDEX <key> <index>

Get the value at the specified index of a list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LINDEX list 0
3

LRANGE <key> <start> <stop>

Get a range of values from a list. If the stop is lower than 0, it will be treated as the last element of the list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LRANGE list 0 1
3
2

LPOS <key> <value>

Get the index of the first occurrence of a value in a list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LPOS list 2
1

LREM <key> <value>

Remove all occurrences of a value from a list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LREM list 2
1

LINSERT <key> <position> <pivot> <value>

Insert a value before or after a pivot in a list.

The pivot should be the value of an existing element, not the index. If the pivot doesn't exist, the command will still run, but do nothing.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LINSERT list BEFORE 2 4
list

LSET <key> <index> <value>

Set the value at the specified index of a list.

Example

> LPUSH list 1
1
 
> LPUSH list 2
2
 
> LPUSH list 3
3
 
> LSET list 1 4
4
 
> LRANGE list 0 2
3
4
1
Last updated on April 25, 2023