Summary
Command Name | LSET |
Usage | Change list value at specific index |
Group | list |
ACL Category | @write @list @slow |
Time Complexity | O(N) |
Flag | WRITE DENYOOM |
Arity | 4 |
Notes
- In the time complexity, N is the length of the list.
- Setting the first and/or last element of the list has a time complexity of O(1).
Signature
LSET <key> <index> <new_item>
Usage
Change/replace value at a specific index of a list. For the command to work there should be an existing list, with some items and we can only change items that are already set(have an index).
Both positive and negative indexes will work for this command.
Notes
- Index is zero(0) based. So the first item is considered at index Zero(0).
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key of the list | key | key |
<index> | Index of list where we want to replace the value | index | integer |
<new_item> | New value that will replace the existing item | element | string |
Return Value
Return value | Case for the return value | Type |
---|---|---|
OK | On successful execution of the command | const |
error | If the index is out of range, the list does not exist or provided key is not of a list | error |
Notes
- If the type of the value in the provided key is not a string then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value - If the provided key of the list does not exist then the following error is returned-
(error) ERR no such key - If the provided <index> is out of range of the list, then the following error is returned-
(error) ERR index out of range
Examples
Here are a few examples of the LSET command usage-
# Redis LSET command examples
# Push some value to list
127.0.0.1:6379> rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
(integer) 5
# Check list
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "Item A"
2) "Item B"
3) "Item C"
4) "Item D"
5) "Item E"
# Set value at index 0
127.0.0.1:6379> lset bigboxlist 0 "Changed item AAAA"
OK
# Set value at index 2 of list
127.0.0.1:6379> lset bigboxlist 2 "Changed item CCCC"
OK
# Set value at index -1 of list
127.0.0.1:6379> lset bigboxlist -1 "Changed item EEEE"
OK
# Check list value
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "Changed item AAAA"
2) "Item B"
3) "Changed item CCCC"
4) "Item D"
5) "Changed item EEEE"
# Try to set value at some out of range index
# error returned
127.0.0.1:6379> lset bigboxlist 200 "Some out of range dummy"
(error) ERR index out of range
# Try to set value at some out of range index
# error returned
127.0.0.1:6379> lset bigboxlist -100 "Another out of range dummy"
(error) ERR index out of range
# Try to use LSET on a non existing list
# We get an error
127.0.0.1:6379> lset nonexistinglist 0 "My value 101"
(error) ERR no such key
# Set some string value
127.0.0.1:6379> set bigboxstr "some string value here"
OK
# Try to use LSET for a string
# error returned as LSET can only be used on a list
127.0.0.1:6379> lset bigboxstr 0 "use lset for str"
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- We can not set any new index using LSET. Only can change/replace existing index value.
Code Implementations
Here are the usage examples of the Redis LSET command in different programming languages.
// Redis LSET command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
// Push some value to list
// Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
// Result: (integer) 5
rpushResult, err := rdb.RPush(ctx, "bigboxlist", "Item A", "Item B", "Item C", "Item D", "Item E").Result()
if err != nil {
fmt.Println("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Error: " + err.Error())
}
fmt.Println("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: ", rpushResult)
// Check list
// Command: lrange bigboxlist 0 -1
// Result:
// 1) "Item A"
// 2) "Item B"
// 3) "Item C"
// 4) "Item D"
// 5) "Item E"
lrangeResult, err := rdb.LRange(ctx, "bigboxlist", 0, -1).Result()
if err != nil {
fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
}
fmt.Println("Command: lrange bigboxlist 0 -1 | Result: ", lrangeResult)
// Set value at index 0
// Command: lset bigboxlist 0 "Changed item AAAA"
// Result: OK
lsetResult, err := rdb.LSet(ctx, "bigboxlist", 0, "Changed item AAAA").Result()
if err != nil {
fmt.Println("Command: lset bigboxlist 0 "Changed item AAAA" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxlist 0 "Changed item AAAA" | Result: " + lsetResult)
// Set value at index 2 of list
// Command: lset bigboxlist 2 "Changed item CCCC"
// Result: OK
lsetResult, err = rdb.LSet(ctx, "bigboxlist", 2, "Changed item CCCC").Result()
if err != nil {
fmt.Println("Command: lset bigboxlist 2 "Changed item CCCC" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxlist 2 "Changed item CCCC" | Result: " + lsetResult)
// Set value at index -1 of list
// Command: lset bigboxlist -1 "Changed item EEEE"
// Result: OK
lsetResult, err = rdb.LSet(ctx, "bigboxlist", -1, "Changed item EEEE").Result()
if err != nil {
fmt.Println("Command: lset bigboxlist -1 "Changed item EEEE" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxlist -1 "Changed item EEEE" | Result: " + lsetResult)
// Check list value
// Command: lrange bigboxlist 0 -1
// Result:
// 1) "Changed item AAAA"
// 2) "Item B"
// 3) "Changed item CCCC"
// 4) "Item D"
// 5) "Changed item EEEE"
lrangeResult, err = rdb.LRange(ctx, "bigboxlist", 0, -1).Result()
if err != nil {
fmt.Println("Command: lrange bigboxlist 0 -1 | Error: " + err.Error())
}
fmt.Println("Command: lrange bigboxlist 0 -1 | Result: ", lrangeResult)
// Try to set value at some out of range index
// error returned
// Command: lset bigboxlist 200 "Some out of range dummy"
// Result: (error) ERR index out of range
lsetResult, err = rdb.LSet(ctx, "bigboxlist", 200, "Some out of range dummy").Result()
if err != nil {
fmt.Println("Command: lset bigboxlist 200 "Some out of range dummy" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxlist 200 "Some out of range dummy" | Result: " + lsetResult)
// Try to set value at some out of range index
// error returned
// Command: lset bigboxlist -100 "Another out of range dummy"
// Result: (error) ERR index out of range
lsetResult, err = rdb.LSet(ctx, "bigboxlist", -200, "Another out of range dummy").Result()
if err != nil {
fmt.Println("Command: lset bigboxlist -100 "Another out of range dummy" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxlist -100 "Another out of range dummy" | Result: " + lsetResult)
// Try to use LSET on a non existing list
// We get an error
// Command: lset nonexistinglist 0 "My value 101"
// Result: (error) ERR no such key
lsetResult, err = rdb.LSet(ctx, "nonexistinglist", 0, "My value 101").Result()
if err != nil {
fmt.Println("Command: lset nonexistinglist 0 "My value 101" | Error: " + err.Error())
}
fmt.Println("Command: lset nonexistinglist 0 "My value 101" | Result: " + lsetResult)
// Set some string value
// Command: set bigboxstr "some string value here"
// Result: OK
setResult, err := rdb.Set(ctx, "bigboxstr", "some string value here", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "some string value here" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "some string value here" | Result: " + setResult)
// Try to use LSET for a string
// error returned as LSET can only be used on a list
// Command: lset bigboxstr 0 "use lset for str"
// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
lsetResult, err = rdb.LSet(ctx, "bigboxstr", 0, "use lset for str").Result()
if err != nil {
fmt.Println("Command: lset bigboxstr 0 "use lset for str" | Error: " + err.Error())
}
fmt.Println("Command: lset bigboxstr 0 "use lset for str" | Result: " + lsetResult)
}
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: [Item A Item B Item C Item D Item E]
Command: lset bigboxlist 0 "Changed item AAAA" | Result: OK
Command: lset bigboxlist 2 "Changed item CCCC" | Result: OK
Command: lset bigboxlist -1 "Changed item EEEE" | Result: OK
Command: lrange bigboxlist 0 -1 | Result: [Changed item AAAA Item B Changed item CCCC Item D Changed item EEEE]
Command: lset bigboxlist 200 "Some out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist 200 "Some out of range dummy" | Result:
Command: lset bigboxlist -100 "Another out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Result:
Command: lset nonexistinglist 0 "My value 101" | Error: ERR no such key
Command: lset nonexistinglist 0 "My value 101" | Result:
Command: set bigboxstr "some string value here" | Result: OK
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lset bigboxstr 0 "use lset for str" | Result:
Notes
- Use “LSet” method from redis-go module.
- Signature of the method is-
func (c cmdable) LSet(ctx context.Context, key string, index int64, value interface{}) *StatusCmd
// Redis LSET command example in JavaScript(NodeJS)
import { createClient } from "redis";
// Create redis client
const redisClient = createClient({
url: "redis://default:@localhost:6379",
});
redisClient.on("error", (err) =>
console.log("Error while connecting to Redis", err)
);
// Connect Redis client
await redisClient.connect();
/**
* Push some value to list
*
* Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
* Result: (integer) 5
*/
let commandResult = await redisClient.rPush("bigboxlist", [
"Item A",
"Item B",
"Item C",
"Item D",
"Item E",
]);
console.log(
'Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: ' +
commandResult
);
/**
* Check list
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Item A"
* 2) "Item B"
* 3) "Item C"
* 4) "Item D"
* 5) "Item E"
*/
commandResult = await redisClient.lRange("bigboxlist", 0, -1);
console.log("Command: lrange bigboxlist 0 -1 | Result: ", commandResult);
/**
* Set value at index 0
*
* Command: lset bigboxlist 0 "Changed item AAAA"
* Result: OK
*/
commandResult = await redisClient.lSet("bigboxlist", 0, "Changed item AAAA");
console.log(
'Command: lset bigboxlist 0 "Changed item AAAA" | Result: ' + commandResult
);
/**
* Set value at index 2 of list
*
* Command: lset bigboxlist 2 "Changed item CCCC"
* Result: OK
*/
commandResult = await redisClient.lSet("bigboxlist", 2, "Changed item CCCC");
console.log(
'Command: lset bigboxlist 2 "Changed item CCCC" | Result: ' + commandResult
);
/**
* Set value at index -1 of list
*
* Command: lset bigboxlist -1 "Changed item EEEE"
* Result: OK
*/
commandResult = await redisClient.lSet("bigboxlist", -1, "Changed item EEEE");
console.log(
'Command: lset bigboxlist -1 "Changed item EEEE" | Result: ' + commandResult
);
/**
* Check list value
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Changed item AAAA"
* 2) "Item B"
* 3) "Changed item CCCC"
* 4) "Item D"
* 5) "Changed item EEEE"
*/
commandResult = await redisClient.lRange("bigboxlist", 0, -1);
console.log("Command: lrange bigboxlist 0 -1 | Result: ", commandResult);
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist 200 "Some out of range dummy"
* Result: (error) ERR index out of range
*/
try {
commandResult = await redisClient.lSet(
"bigboxlist",
200,
"Some out of range dummy"
);
console.log(
'Command: lset bigboxlist 200 "Some out of range dummy" | Result: ' +
commandResult
);
} catch (err) {
console.log(
'Command: lset bigboxlist 200 "Some out of range dummy" | Error: ',
err
);
}
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist -100 "Another out of range dummy"
* Result: (error) ERR index out of range
*/
try {
commandResult = await redisClient.lSet(
"bigboxlist",
-200,
"Another out of range dummy"
);
console.log(
'Command: lset bigboxlist -100 "Another out of range dummy" | Result: ' +
commandResult
);
} catch (err) {
console.log(
'Command: lset bigboxlist -100 "Another out of range dummy" | Error: ',
err
);
}
/**
* Try to use LSET on a non existing list
* We get an error
*
* Command: lset nonexistinglist 0 "My value 101"
* Result: (error) ERR no such key
*/
try {
commandResult = await redisClient.lSet("nonexistinglist", 0, "My value 101");
console.log(
'Command: lset nonexistinglist 0 "My value 101" | Result: ' + commandResult
);
} catch (err) {
console.log('Command: lset nonexistinglist 0 "My value 101" | Error: ', err);
}
/**
* Set some string value
*
* Command: set bigboxstr "some string value here"
* Result: OK
*/
commandResult = await redisClient.set("bigboxstr", "some string value here");
console.log(
'Command: set bigboxstr "some string value here" | Result: ' + commandResult
);
/**
* Try to use LSET for a string
* error returned as LSET can only be used on a list
*
* Command: lset bigboxstr 0 "use lset for str"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.lSet("bigboxstr", 0, "use lset for str");
console.log(
'Command: lset bigboxstr 0 "use lset for str" | Result: ' + commandResult
);
} catch (err) {
console.log('Command: lset bigboxstr 0 "use lset for str" | Error: ', err);
}
process.exit(0);
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: [ 'Item A', 'Item B', 'Item C', 'Item D', 'Item E' ]
Command: lset bigboxlist 0 "Changed item AAAA" | Result: OK
Command: lset bigboxlist 2 "Changed item CCCC" | Result: OK
Command: lset bigboxlist -1 "Changed item EEEE" | Result: OK
Command: lrange bigboxlist 0 -1 | Result: [
'Changed item AAAA',
'Item B',
'Changed item CCCC',
'Item D',
'Changed item EEEE'
]
Command: lset bigboxlist 200 "Some out of range dummy" | Error: [ErrorReply: ERR index out of range]
Command: lset bigboxlist -100 "Another out of range dummy" | Error: [ErrorReply: ERR index out of range]
Command: lset nonexistinglist 0 "My value 101" | Error: [ErrorReply: ERR no such key]
Command: set bigboxstr "some string value here" | Result: OK
Command: lset bigboxstr 0 "use lset for str" | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use the function “lSet” of node-redis.
- Signature of the method-
lSet(key: RedisCommandArgument, index: number, element: RedisCommandArgument)
// Redis LSET Command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
public class Lset {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Push some value to list
*
* Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
* Result: (integer) 5
*/
long rpushResult = jedis.rpush("bigboxlist", "Item A", "Item B", "Item C", "Item D", "Item E");
System.out.println("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: " + rpushResult);
/**
* Check list
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Item A"
* 2) "Item B"
* 3) "Item C"
* 4) "Item D"
* 5) "Item E"
*/
List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);
System.out.println("Command: lrange bigboxlist 0 -1 | Result: " + lrangeResult.toString());
/**
* Set value at index 0
*
* Command: lset bigboxlist 0 "Changed item AAAA"
* Result: OK
*/
String lsetResult = jedis.lset("bigboxlist", 0, "Changed item AAAA");
System.out.println("Command: lset bigboxlist 0 "Changed item AAAA" | Result: " + lsetResult);
/**
* Set value at index 2 of list
*
* Command: lset bigboxlist 2 "Changed item CCCC"
* Result: OK
*/
lsetResult = jedis.lset("bigboxlist", 2, "Changed item CCCC");
System.out.println("Command: lset bigboxlist 2 "Changed item CCCC" | Result: " + lsetResult);
/**
* Set value at index -1 of list
*
* Command: lset bigboxlist -1 "Changed item EEEE"
* Result: OK
*/
lsetResult = jedis.lset("bigboxlist", -1, "Changed item EEEE");
System.out.println("Command: lset bigboxlist -1 "Changed item EEEE" | Result: " + lsetResult);
/**
* Check list value
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Changed item AAAA"
* 2) "Item B"
* 3) "Changed item CCCC"
* 4) "Item D"
* 5) "Changed item EEEE"
*/
lrangeResult = jedis.lrange("bigboxlist", 0, -1);
System.out.println("Command: lrange bigboxlist 0 -1 | Result: " + lrangeResult.toString());
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist 200 "Some out of range dummy"
* Result: (error) ERR index out of range
*/
try {
lsetResult = jedis.lset("bigboxlist", 200, "Some out of range dummy");
System.out.println("Command: lset bigboxlist 200 "Some out of range dummy" | Result: " + lsetResult);
} catch(Exception e) {
System.out.println("Command: lset bigboxlist 200 "Some out of range dummy" | Error: " + e.getMessage());
}
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist -100 "Another out of range dummy"
* Result: (error) ERR index out of range
*/
try {
lsetResult = jedis.lset("bigboxlist", -200, "Another out of range dummy");
System.out.println("Command: lset bigboxlist -100 "Another out of range dummy" | Result: " + lsetResult);
} catch(Exception e) {
System.out.println("Command: lset bigboxlist -100 "Another out of range dummy" | Error: " + e.getMessage());
}
/**
* Try to use LSET on a non existing list
* We get an error
*
* Command: lset nonexistinglist 0 "My value 101"
* Result: (error) ERR no such key
*/
try {
lsetResult = jedis.lset("nonexistinglist", 0, "My value 101");
System.out.println("Command: lset nonexistinglist 0 "My value 101" | Result: " + lsetResult);
} catch(Exception e) {
System.out.println("Command: lset nonexistinglist 0 "My value 101" | Error: " + e.getMessage());
}
/**
* Set some string value
*
* Command: set bigboxstr "some string value here"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "some string value here");
System.out.println("Command: set bigboxstr "some string value here" | Result: " + setResult);
/**
* Try to use LSET for a string
* error returned as LSET can only be used on a list
*
* Command: lset bigboxstr 0 "use lset for str"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
lsetResult = jedis.lset("bigboxstr", 0, "use lset for str");
System.out.println("Command: lset bigboxstr 0 "use lset for str" | Result: " + lsetResult);
} catch(Exception e) {
System.out.println("Command: lset bigboxstr 0 "use lset for str" | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: [Item A, Item B, Item C, Item D, Item E]
Command: lset bigboxlist 0 "Changed item AAAA" | Result: OK
Command: lset bigboxlist 2 "Changed item CCCC" | Result: OK
Command: lset bigboxlist -1 "Changed item EEEE" | Result: OK
Command: lrange bigboxlist 0 -1 | Result: [Changed item AAAA, Item B, Changed item CCCC, Item D, Changed item EEEE]
Command: lset bigboxlist 200 "Some out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Error: ERR index out of range
Command: lset nonexistinglist 0 "My value 101" | Error: ERR no such key
Command: set bigboxstr "some string value here" | Result: OK
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lset” from Jedis package.
- The signature of the method is-
public String lset(final String key, final long index, final String value)
// Redis LSET command examples in C#
using StackExchange.Redis;
namespace Lset
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Push some value to list
*
* Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
* Result: (integer) 5
*/
long rpushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "Item A", "Item B", "Item C", "Item D", "Item E" });
Console.WriteLine("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: " + rpushResult);
/**
* Check list
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Item A"
* 2) "Item B"
* 3) "Item C"
* 4) "Item D"
* 5) "Item E"
*/
RedisValue[] lrangeResult = rdb.ListRange("bigboxlist", 0, -1);
Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result: " + string.Join(", ", lrangeResult));
/**
* Set value at index 0
*
* Command: lset bigboxlist 0 "Changed item AAAA"
* Result: OK
*/
rdb.ListSetByIndex("bigboxlist", 0, "Changed item AAAA");
Console.WriteLine("Command: lset bigboxlist 0 "Changed item AAAA"");
/**
* Set value at index 2 of list
*
* Command: lset bigboxlist 2 "Changed item CCCC"
* Result: OK
*/
rdb.ListSetByIndex("bigboxlist", 2, "Changed item CCCC");
Console.WriteLine("Command: lset bigboxlist 2 "Changed item CCCC"");
/**
* Set value at index -1 of list
*
* Command: lset bigboxlist -1 "Changed item EEEE"
* Result: OK
*/
rdb.ListSetByIndex("bigboxlist", -1, "Changed item EEEE");
Console.WriteLine("Command: lset bigboxlist -1 "Changed item EEEE"");
/**
* Check list value
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Changed item AAAA"
* 2) "Item B"
* 3) "Changed item CCCC"
* 4) "Item D"
* 5) "Changed item EEEE"
*/
lrangeResult = rdb.ListRange("bigboxlist", 0, -1);
Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result: " + string.Join(", ", lrangeResult));
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist 200 "Some out of range dummy"
* Result: (error) ERR index out of range
*/
try
{
rdb.ListSetByIndex("bigboxlist", 200, "Some out of range dummy");
Console.WriteLine("Command: lset bigboxlist 200 "Some out of range dummy"");
}
catch (Exception e)
{
Console.WriteLine("Command: lset bigboxlist 200 "Some out of range dummy" | Error: " + e.Message);
}
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist -100 "Another out of range dummy"
* Result: (error) ERR index out of range
*/
try
{
rdb.ListSetByIndex("bigboxlist", -200, "Another out of range dummy");
Console.WriteLine("Command: lset bigboxlist -100 "Another out of range dummy"");
}
catch (Exception e)
{
Console.WriteLine("Command: lset bigboxlist -100 "Another out of range dummy" | Error: " + e.Message);
}
/**
* Try to use LSET on a non existing list
* We get an error
*
* Command: lset nonexistinglist 0 "My value 101"
* Result: (error) ERR no such key
*/
try
{
rdb.ListSetByIndex("nonexistinglist", 0, "My value 101");
Console.WriteLine("Command: lset nonexistinglist 0 "My value 101"");
}
catch (Exception e)
{
Console.WriteLine("Command: lset nonexistinglist 0 "My value 101" | Error: " + e.Message);
}
/**
* Set some string value
*
* Command: set bigboxstr "some string value here"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "some string value here");
Console.WriteLine("Command: set bigboxstr "some string value here" | Result: " + setResult);
/**
* Try to use LSET for a string
* error returned as LSET can only be used on a list
*
* Command: lset bigboxstr 0 "use lset for str"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
rdb.ListSetByIndex("bigboxstr", 0, "use lset for str");
Console.WriteLine("Command: lset bigboxstr 0 "use lset for str"");
}
catch (Exception e)
{
Console.WriteLine("Command: lset bigboxstr 0 "use lset for str" | Error: " + e.Message);
}
}
}
}
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: Item A, Item B, Item C, Item D, Item E
Command: lset bigboxlist 0 "Changed item AAAA"
Command: lset bigboxlist 2 "Changed item CCCC"
Command: lset bigboxlist -1 "Changed item EEEE"
Command: lrange bigboxlist 0 -1 | Result: Changed item AAAA, Item B, Changed item CCCC, Item D, Changed item EEEE
Command: lset bigboxlist 200 "Some out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Error: ERR index out of range
Command: lset nonexistinglist 0 "My value 101" | Error: ERR no such key
Command: set bigboxstr "some string value here" | Result: True
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “ListSetByIndex” from StackExchange.Redis.
- Signature of the method is-
void ListSetByIndex(RedisKey key, long index, RedisValue value, CommandFlags flags = CommandFlags.None)
<?php
// Redis LSET command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Push some value to list
*
* Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
* Result: (integer) 5
*/
$commandResult = $redisClient->rpush("bigboxlist", ["Item A", "Item B", "Item C", "Item D", "Item E"]);
echo "Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: " . $commandResult . "n";
/**
* Check list
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Item A"
* 2) "Item B"
* 3) "Item C"
* 4) "Item D"
* 5) "Item E"
*/
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);
echo "Command: lrange bigboxlist 0 -1 | Result: ";
print_r($commandResult);
/**
* Set value at index 0
*
* Command: lset bigboxlist 0 "Changed item AAAA"
* Result: OK
*/
$commandResult = $redisClient->lset("bigboxlist", 0, "Changed item AAAA");
echo "Command: lset bigboxlist 0 "Changed item AAAA" | Result: " . $commandResult . "n";
/**
* Set value at index 2 of list
*
* Command: lset bigboxlist 2 "Changed item CCCC"
* Result: OK
*/
$commandResult = $redisClient->lset("bigboxlist", 2, "Changed item CCCC");
echo "Command: lset bigboxlist 2 "Changed item CCCC" | Result: " . $commandResult . "n";
/**
* Set value at index -1 of list
*
* Command: lset bigboxlist -1 "Changed item EEEE"
* Result: OK
*/
$commandResult = $redisClient->lset("bigboxlist", -1, "Changed item EEEE");
echo "Command: lset bigboxlist -1 "Changed item EEEE" | Result: " . $commandResult . "n";
/**
* Check list value
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "Changed item AAAA"
* 2) "Item B"
* 3) "Changed item CCCC"
* 4) "Item D"
* 5) "Changed item EEEE"
*/
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);
echo "Command: lrange bigboxlist 0 -1 | Result: ";
print_r($commandResult);
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist 200 "Some out of range dummy"
* Result: (error) ERR index out of range
*/
try {
$commandResult = $redisClient->lset("bigboxlist", 200, "Some out of range dummy");
echo "Command: lset bigboxlist 200 "Some out of range dummy" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: lset bigboxlist 200 "Some out of range dummy" | Error: " . $e->getMessage() . "n";
}
/**
* Try to set value at some out of range index
* error returned
*
* Command: lset bigboxlist -100 "Another out of range dummy"
* Result: (error) ERR index out of range
*/
try {
$commandResult = $redisClient->lset("bigboxlist", -200, "Another out of range dummy");
echo "Command: lset bigboxlist -100 "Another out of range dummy" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: lset bigboxlist -100 "Another out of range dummy" | Error: " . $e->getMessage() . "n";
}
/**
* Try to use LSET on a non existing list
* We get an error
*
* Command: lset nonexistinglist 0 "My value 101"
* Result: (error) ERR no such key
*/
try {
$commandResult = $redisClient->lset("nonexistinglist", 0, "My value 101");
echo "Command: lset nonexistinglist 0 "My value 101" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: lset nonexistinglist 0 "My value 101" | Error: " . $e->getMessage() . "n";
}
/**
* Set some string value
*
* Command: set bigboxstr "some string value here"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "some string value here");
echo "Command: set bigboxstr "some string value here" | Result: " . $commandResult . "n";
/**
* Try to use LSET for a string
* error returned as LSET can only be used on a list
*
* Command: lset bigboxstr 0 "use lset for str"
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->lset("bigboxstr", 0, "use lset for str");
echo "Command: lset bigboxstr 0 "use lset for str" | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: lset bigboxstr 0 "use lset for str" | Error: " . $e->getMessage() . "n";
}
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: Array
(
[0] => Item A
[1] => Item B
[2] => Item C
[3] => Item D
[4] => Item E
)
Command: lset bigboxlist 0 "Changed item AAAA" | Result: OK
Command: lset bigboxlist 2 "Changed item CCCC" | Result: OK
Command: lset bigboxlist -1 "Changed item EEEE" | Result: OK
Command: lrange bigboxlist 0 -1 | Result: Array
(
[0] => Changed item AAAA
[1] => Item B
[2] => Changed item CCCC
[3] => Item D
[4] => Changed item EEEE
)
Command: lset bigboxlist 200 "Some out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Error: ERR index out of range
Command: lset nonexistinglist 0 "My value 101" | Error: ERR no such key
Command: set bigboxstr "some string value here" | Result: OK
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “lset” of predis.
- Signature of the method is-
lset(string $key, int $index, string $value): mixed
# Redis LSET command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Push some value to list
# Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
# Result: (integer) 5
commandResult = redisClient.rpush("bigboxlist", "Item A", "Item B", "Item C", "Item D", "Item E")
print("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: {}".format(commandResult))
# Check list
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "Item A"
# 2) "Item B"
# 3) "Item C"
# 4) "Item D"
# 5) "Item E"
commandResult = redisClient.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result: {}".format(commandResult))
# Set value at index 0
# Command: lset bigboxlist 0 "Changed item AAAA"
# Result: OK
commandResult = redisClient.lset("bigboxlist", 0, "Changed item AAAA")
print("Command: lset bigboxlist 0 "Changed item AAAA" | Result: {}".format(commandResult))
# Set value at index 2 of list
# Command: lset bigboxlist 2 "Changed item CCCC"
# Result: OK
commandResult = redisClient.lset("bigboxlist", 2, "Changed item CCCC")
print("Command: lset bigboxlist 2 "Changed item CCCC" | Result: {}".format(commandResult))
# Set value at index -1 of list
# Command: lset bigboxlist -1 "Changed item EEEE"
# Result: OK
commandResult = redisClient.lset("bigboxlist", -1, "Changed item EEEE")
print("Command: lset bigboxlist -1 "Changed item EEEE" | Result: {}".format(commandResult))
# Check list value
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "Changed item AAAA"
# 2) "Item B"
# 3) "Changed item CCCC"
# 4) "Item D"
# 5) "Changed item EEEE"
commandResult = redisClient.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result: {}".format(commandResult))
# Try to set value at some out of range index
# error returned
# Command: lset bigboxlist 200 "Some out of range dummy"
# Result: (error) ERR index out of range
try:
commandResult = redisClient.lset("bigboxlist", 200, "Some out of range dummy")
print("Command: lset bigboxlist 200 "Some out of range dummy" | Result: {}".format(commandResult))
except Exception as error:
print("Command: lset bigboxlist 200 "Some out of range dummy" | Error: ", error)
# Try to set value at some out of range index
# error returned
# Command: lset bigboxlist -100 "Another out of range dummy"
# Result: (error) ERR index out of range
try:
commandResult = redisClient.lset("bigboxlist", -200, "Another out of range dummy")
print("Command: lset bigboxlist -100 "Another out of range dummy" | Result: {}".format(commandResult))
except Exception as error:
print("Command: lset bigboxlist -100 "Another out of range dummy" | Error: ", error)
# Try to use LSET on a non existing list
# We get an error
# Command: lset nonexistinglist 0 "My value 101"
# Result: (error) ERR no such key
try:
commandResult = redisClient.lset("nonexistinglist", 0, "My value 101")
print("Command: lset nonexistinglist 0 "My value 101" | Result: {}".format(commandResult))
except Exception as error:
print("Command: lset nonexistinglist 0 "My value 101" | Error: ", error)
# Set some string value
# Command: set bigboxstr "some string value here"
# Result: OK
commandResult = redisClient.set("bigboxstr", "some string value here")
print("Command: set bigboxstr "some string value here" | Result: {}".format(commandResult))
# Try to use LSET for a string
# error returned as LSET can only be used on a list
# Command: lset bigboxstr 0 "use lset for str"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.lset("bigboxstr", 0, "use lset for str")
print("Command: lset bigboxstr 0 "use lset for str" | Result: {}".format(commandResult))
except Exception as error:
print("Command: lset bigboxstr 0 "use lset for str" | Error: ", error)
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: ['Item A', 'Item B', 'Item C', 'Item D', 'Item E']
Command: lset bigboxlist 0 "Changed item AAAA" | Result: True
Command: lset bigboxlist 2 "Changed item CCCC" | Result: True
Command: lset bigboxlist -1 "Changed item EEEE" | Result: True
Command: lrange bigboxlist 0 -1 | Result: ['Changed item AAAA', 'Item B', 'Changed item CCCC', 'Item D', 'Changed item EEEE']
Command: lset bigboxlist 200 "Some out of range dummy" | Error: index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Error: index out of range
Command: lset nonexistinglist 0 "My value 101" | Error: no such key
Command: set bigboxstr "some string value here" | Result: True
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lset” from redis-py.
- Signature of the method is –
def lset(self, name: str, index: int, value: str) -> Union[Awaitable[str], str]
# Redis LSET command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Push some value to list
# Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E"
# Result: (integer) 5
commandResult = redis.rpush("bigboxlist", ["Item A", "Item B", "Item C", "Item D", "Item E"])
print("Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: ", commandResult, "n")
# Check list
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "Item A"
# 2) "Item B"
# 3) "Item C"
# 4) "Item D"
# 5) "Item E"
commandResult = redis.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result: ", commandResult, "n")
# Set value at index 0
# Command: lset bigboxlist 0 "Changed item AAAA"
# Result: OK
commandResult = redis.lset("bigboxlist", 0, "Changed item AAAA")
print("Command: lset bigboxlist 0 "Changed item AAAA" | Result: ", commandResult, "n")
# Set value at index 2 of list
# Command: lset bigboxlist 2 "Changed item CCCC"
# Result: OK
commandResult = redis.lset("bigboxlist", 2, "Changed item CCCC")
print("Command: lset bigboxlist 2 "Changed item CCCC" | Result: ", commandResult, "n")
# Set value at index -1 of list
# Command: lset bigboxlist -1 "Changed item EEEE"
# Result: OK
commandResult = redis.lset("bigboxlist", -1, "Changed item EEEE")
print("Command: lset bigboxlist -1 "Changed item EEEE" | Result: ", commandResult, "n")
# Check list value
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "Changed item AAAA"
# 2) "Item B"
# 3) "Changed item CCCC"
# 4) "Item D"
# 5) "Changed item EEEE"
commandResult = redis.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result: ", commandResult, "n")
# Try to set value at some out of range index
# error returned
# Command: lset bigboxlist 200 "Some out of range dummy"
# Result: (error) ERR index out of range
begin
commandResult = redis.lset("bigboxlist", 200, "Some out of range dummy")
print("Command: lset bigboxlist 200 "Some out of range dummy" | Result: ", commandResult, "n")
rescue => e
print("Command: lset bigboxlist 200 "Some out of range dummy" | Error: ", e, "n")
end
# Try to set value at some out of range index
# error returned
# Command: lset bigboxlist -100 "Another out of range dummy"
# Result: (error) ERR index out of range
begin
commandResult = redis.lset("bigboxlist", -200, "Another out of range dummy")
print("Command: lset bigboxlist -100 "Another out of range dummy" | Result: ", commandResult, "n")
rescue => e
print("Command: lset bigboxlist -100 "Another out of range dummy" | Error: ", e, "n")
end
# Try to use LSET on a non existing list
# We get an error
# Command: lset nonexistinglist 0 "My value 101"
# Result: (error) ERR no such key
begin
commandResult = redis.lset("nonexistinglist", 0, "My value 101")
print("Command: lset nonexistinglist 0 "My value 101" | Result: ", commandResult, "n")
rescue => e
print("Command: lset nonexistinglist 0 "My value 101" | Error: ", e, "n")
end
# Set some string value
# Command: set bigboxstr "some string value here"
# Result: OK
commandResult = redis.set("bigboxstr", "some string value here")
print("Command: set bigboxstr "some string value here" | Result: ", commandResult, "n")
# Try to use LSET for a string
# error returned as LSET can only be used on a list
# Command: lset bigboxstr 0 "use lset for str"
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.lset("bigboxstr", 0, "use lset for str")
print("Command: lset bigboxstr 0 "use lset for str" | Result: ", commandResult, "n")
rescue => e
print("Command: lset bigboxstr 0 "use lset for str" | Error: ", e, "n")
end
Output:
Command: rpush bigboxlist "Item A" "Item B" "Item C" "Item D" "Item E" | Result: 5
Command: lrange bigboxlist 0 -1 | Result: ["Item A", "Item B", "Item C", "Item D", "Item E"]
Command: lset bigboxlist 0 "Changed item AAAA" | Result: OK
Command: lset bigboxlist 2 "Changed item CCCC" | Result: OK
Command: lset bigboxlist -1 "Changed item EEEE" | Result: OK
Command: lrange bigboxlist 0 -1 | Result: ["Changed item AAAA", "Item B", "Changed item CCCC", "Item D", "Changed item EEEE"]
Command: lset bigboxlist 200 "Some out of range dummy" | Error: ERR index out of range
Command: lset bigboxlist -100 "Another out of range dummy" | Error: ERR index out of range
Command: lset nonexistinglist 0 "My value 101" | Error: ERR no such key
Command: set bigboxstr "some string value here" | Result: OK
Command: lset bigboxstr 0 "use lset for str" | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lset” from the redis-rb.
- Signature of the method is-
# @param [String] key
# @param [Integer] index
# @param [String] value
# @return [String] `OK`
def lset(key, index, value)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
LRANGE | Command Details |
LPUSH | Command Details |
RPUSH | Command Details |