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

2 months ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a new element in the list value stored in redis datastore. For this, we will use Redis LSET command.

LSET Command

This commands set a new element at the specified index of a list value stored at a key. The index is zero based, so 0 means the first element, 1 means the second element and so on. A negative number can also be used to designate the indexes of the list, where -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. The syntax of the 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.

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.

<- List Commands