# LSET

> Replace the element at an index of a list.

URL: https://docs.hotkv.com/commands/lset/

```
LSET key index element
```

- Group: List
- Complexity: O(N) where N is the distance of the index from the nearer end
- 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.

## Return value

| 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`.

## Examples

```
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"
```

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[LINDEX](https://docs.hotkv.com/commands/lindex/index.md), [LINSERT](https://docs.hotkv.com/commands/linsert/index.md), [LRANGE](https://docs.hotkv.com/commands/lrange/index.md)