Redis LSET – How to set element at specific index of a list in redis

5 days ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a new element at the specified index of a list value. For this we will use a COMMAND – LSET in redis-cli. The syntax of redis LSET command is as follows :-

Syntax :-

redis host:post> LSET <key name> <index> <element>

Output :- 

- OK, if <element> is successfully set in the list.
- (error) ERR index out of range, if <index> value is out of range.
- Error, if key exist and value stored at the key is not a list.

The index is zero based, so 0 means the first element, 1 means the second element and so on. Negative number can also be used to designate the indexes of the list, here -1 means the last element, -2 means second last element and so on. An error is returned when LSET command is called with an index value which is out of the range.

Example :-

Redis LSET

References :-

  1. LSET Command Docs

That’s all for how to set an element at a specific index of the list value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.