Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

LSET

Last updated
LSET key index element
Group
List
Complexity
O(N) where N is the distance of the index from the nearer end
Arity
4 (exactly, counting the command name)
Flags
write
Keys
first 1, last 1, step 1
HotKV Cloud
All plans
On-Premise
no license needed

LSET overwrites the element at position index of the list stored at key with element. Indexes start at 0; negative indexes count from the end. The list must exist and the index must be in range.

Case RESP2 RESP3
Success Simple string OK Same

Errors:

  • ERR no such key if key does not exist.
  • WRONGTYPE if key holds a value that is not a list.
  • ERR value is not an integer or out of range if index is not an integer.
  • ERR index out of range if the list has no element at index.
127.0.0.1:6379> RPUSH letters a b c
(integer) 3
127.0.0.1:6379> LSET letters 1 "B"
OK
127.0.0.1:6379> LSET letters -1 "C"
OK
127.0.0.1:6379> LSET letters 9 "z"
(error) ERR index out of range
127.0.0.1:6379> LRANGE letters 0 -1
1) "a"
2) "B"
3) "C"

Same as Redis 7.4 and Valkey 8.

LINDEX, LINSERT, LRANGE