w3resource

Redis Lists: LSET

Redis LSET Command

Redis LSET command is used to set the list element at index to value. The index is zero-based, so 0 means the first element, 1 the second element and so on. Negative indices can be used to designate elements starting at the tail of the list.

Here are some common uses and scenarios for the "LSET" command:

  • Element Update: Update the value of an element at a specific index in a list.
  • Data Modification: Modify list elements without changing their positions.
  • Configuration Management: Update configuration values stored in a list by index.
  • Session Management: Modify session data or user attributes stored in lists based on their position.
  • Data Validation: Ensure data integrity by setting specific values at known indices in a list.

Syntax:

LSET KEY_NAME INDEX VALUE

Available since

1.0.0.

Return Value

String reply, OK

Return Value Type

String

Example: Redis LSET

127.0.0.1:6379> LPUSH mycolor1 white black red blue
(integer) 4
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "blue"
2) "red"
3) "black"
4) "white"
127.0.0.1:6379> LSET mycolor1 2 YELLOW
OK
127.0.0.1:6379> LSET mycolor1 -1 GREEN
OK
127.0.0.1:6379> LRANGE mycolor1 0 -1
1) "blue"
2) "red"
3) "YELLOW"
4) "GREEN"

127.0.0.1:6379> LSET mycolor 2 YELLOW
(error) ERR index out of range

Previous: LREM
Next: LRANGE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-lset-key-index-value.php