LSET · Valkey

LSET

Sets the value of an element in a list by its index.

Usage

LSET key index element

Description

Sets the list element at index to element. For more information on the index argument, see LINDEX.

An error is returned for out of range indexes.

Reply

Simple string reply: OK.

Complexity

O(N) where N is the length of the list. Setting either the first or the last element of the list is O(1).

ACL Categories

@list @slow @write

Examples

127.0.0.1:6379> RPUSH mylist "one"
(integer) 1
127.0.0.1:6379> RPUSH mylist "two"
(integer) 2
127.0.0.1:6379> RPUSH mylist "three"
(integer) 3
127.0.0.1:6379> LSET mylist 0 "four"
OK
127.0.0.1:6379> LSET mylist -2 "five"
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "four"
2) "five"
3) "three"

History

See also

BLMOVE, BLMPOP, BLPOP, BRPOP, LINDEX, LINSERT, LLEN, LMOVE, LMPOP, LPOP, LPOS, LPUSH, LPUSHX, LRANGE, LREM, LTRIM, RPOP, RPUSH, RPUSHX.