Summary
Command Name | LINDEX |
Usage | Get list item by index |
Group | list |
ACL Category | @read @list @slow |
Time Complexity | O(N) ** N is the distance of the index |
Flag | READONLY |
Arity | 3 |
Notes
- Time complexity of this command is O(N). Here N is the number of elements that we need to traverse to reach to the index from any end.
- For the first and last element of the list, the time complexity is O(1), as we get the item just when we start the operation, and we don’t need to go through a bunch of items to react to the index.
Signature
LINDEX <key> <index>
Usage
Get an element from a list at a specific index.
Time complexity of this command is O(N). Here N is the number of items that we have to go through to get the specified index. In the case of positive indexing, the counting starts from the HEAD/Left. In the case of negative indexing, the counting starts from the TAIL/Right end.
Notes
- Time complexity to get the first and last item is O(1), as those are the first elements from the left/right end.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Key name of the list | key | key |
<index> | Index of the desired item | index | integer |
Notes
- Indexing in the list is zero-based. The first item is considered at index Zero(0).
- In the case of negative indexing, the last index is considered to be index negative one(-1).
Return Value
Return value | Case for the return value | Type |
---|---|---|
List item at specific index | If the operation was successful | string |
(nil) | Index is out of range or list (key) does not exist | null |
error | If the data type of source and/or destination is not of type list | error |
Notes
- If key is not of type list, but some other data type, then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Examples
Here are a few examples of the LINDEX command usage-
# Redis LINDEX command examples
# Create list and push items
127.0.0.1:6379> rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
(integer) 10
# Check list items
127.0.0.1:6379> lrange bigboxlist 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
6) "test a"
7) "test b"
8) "test c"
9) "second last item"
10) "last item"
# Get list item at index Zero(0)
127.0.0.1:6379> lindex bigboxlist 0
"one"
# Get list item at index One(1)
127.0.0.1:6379> lindex bigboxlist 1
"two"
# Get list item at index Five(5)
127.0.0.1:6379> lindex bigboxlist 5
"test a"
# Get list item at index Negative One(-1)
# The last item in list
127.0.0.1:6379> lindex bigboxlist -1
"last item"
# Get list item at index Negative Two(-2)
# The second last item in list
127.0.0.1:6379> lindex bigboxlist -2
"second last item"
# Try to get item at index out of index
# Returns (nil), if index is out of range
127.0.0.1:6379> lindex bigboxlist 100000000
(nil)
# Try to get item at index out of index
# Returns (nil), if index is out of range
127.0.0.1:6379> lindex bigboxlist -99999999999
(nil)
# Try to get list item, when the list does not exist
# Returns (nil)
127.0.0.1:6379> lindex nonexistingkey 0
(nil)
# Set a string key
127.0.0.1:6379> set firststr "some string value here"
OK
# Try to use LINDEX for an element that is not a list
# We get an error in that case
127.0.0.1:6379> lindex firststr 0
(error) WRONGTYPE Operation against a key holding the wrong kind of value
# Pass a very large(larger than 64-bit) integer as index
# We get an error related to index being an integer
127.0.0.1:6379> lindex bigboxlist 14293742398742398423489723984327498327
(error) ERR value is not an integer or out of range
Notes
- If we pass an integer value as index, which is out of the range of a 64-bit integer, we get following error-
(error) ERR value is not an integer or out of range
This error is not related to the list, it is specifically related to the size/range of integer value of the index.
Code Implementations
Here are the usage examples of the Redis LRANGE command in different programming languages.
// Redis LINDEX 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() {
// Create list and push items
// Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
// Result: (integer) 10
pushResult, err := rdb.RPush(ctx, "bigboxlist", "one", "two", "three", "four", "five", "test a", "test b", "test c", "second last item", "last item").Result()
if err != nil {
fmt.Println("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: Error: " + err.Error())
}
fmt.Printf("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: %vn", pushResult)
// Check list items
// Command: lrange bigboxlist 0 -1
// Result:
// 1) "one"
// 2) "two"
// 3) "three"
// 4) "four"
// 5) "five"
// 6) "test a"
// 7) "test b"
// 8) "test c"
// 9) "second last item"
// 10) "last item"
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:")
for _, item := range lrangeResult {
fmt.Println(item)
}
// Get list item at index Zero(0)
// Command: lindex bigboxlist 0
// Result: "one"
lindexResult, err := rdb.LIndex(ctx, "bigboxlist", 0).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist 0 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist 0 | Result: " + lindexResult)
// Get list item at index One(1)
// Command: lindex bigboxlist 1
// Result: "two"
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", 1).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist 1 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist 1 | Result: " + lindexResult)
// Get list item at index Five(5)
// Command: lindex bigboxlist 5
// Result: "test a"
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", 5).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist 5 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist 5 | Result: " + lindexResult)
// Get list item at index Negative One(-1)
// The last item in list
// Command: lindex bigboxlist -1
// Result: "last item"
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", -1).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist -1 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist -1 | Result: " + lindexResult)
// Get list item at index Negative Two(-2)
// The second last item in list
// Command: lindex bigboxlist -2
// Result: "second last item"
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", -2).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist -2 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist -2 | Result: " + lindexResult)
// Try to get item at index out of index
// Returns (nil), if index is out of range
// Command: lindex bigboxlist 100000000
// Result: (nil)
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", 100000000).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist 100000000 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist 100000000 | Result: " + lindexResult)
// Try to get item at index out of index
// Returns (nil), if index is out of range
// Command: lindex bigboxlist -99999999
// Result: (nil)
lindexResult, err = rdb.LIndex(ctx, "bigboxlist", -99999999).Result()
if err != nil {
fmt.Println("Command: lindex bigboxlist -99999999 | Error: " + err.Error())
}
fmt.Println("Command: lindex bigboxlist -99999999 | Result: " + lindexResult)
// Try to get list item, when the list does not exist
// Returns (nil)
// Command: lindex nonexistingkey 0
// Result: (nil)
lindexResult, err = rdb.LIndex(ctx, "nonexistingkey", 0).Result()
if err != nil {
fmt.Println("Command: lindex nonexistingkey 0 | Error: " + err.Error())
}
fmt.Println("Command: lindex nonexistingkey 0 | Result: " + lindexResult)
// Set a string key
// Command: set firststr "some string value here"
// Result: OK
setResult, err := rdb.Set(ctx, "firststr", "some string value here", 0).Result()
if err != nil {
fmt.Println("Command: set firststr "some string value here" | Error: " + err.Error())
}
fmt.Println("Command: set firststr "some string value here" | Result: " + setResult)
// Try to use LINDEX for an element that is not a list
// We get an error in that case
// Command: lindex firststr 0
// Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
lindexResult, err = rdb.LIndex(ctx, "firststr", 0).Result()
if err != nil {
fmt.Println("Command: lindex firststr 0 | Error: " + err.Error())
}
fmt.Println("Command: lindex firststr 0 | Result: " + lindexResult)
}
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
test a
test b
test c
second last item
last item
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Error: redis: nil
Command: lindex bigboxlist 100000000 | Result:
Command: lindex bigboxlist -99999999 | Error: redis: nil
Command: lindex bigboxlist -99999999 | Result:
Command: lindex nonexistingkey 0 | Error: redis: nil
Command: lindex nonexistingkey 0 | Result:
Command: set firststr "some string value here" | Result: OK
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: lindex firststr 0 | Result:
Notes
- Use “LIndex” method from redis-go module.
- Signature of the method is-
LIndex(ctx context.Context, key string, index int64) *StringCmd
// Redis LINDEX 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();
/**
* Create list and push items
*
* Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
* Result: (integer) 10
*/
let commandResult = await redisClient.rPush("bigboxlist", [
"one",
"two",
"three",
"four",
"five",
"test a",
"test b",
"test c",
"second last item",
"last item",
]);
console.log(
'Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: ' +
commandResult
);
/**
* Check list items
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
* 4) "four"
* 5) "five"
* 6) "test a"
* 7) "test b"
* 8) "test c"
* 9) "second last item"
* 10) "last item"
*/
commandResult = await redisClient.lRange("bigboxlist", 0, -1);
console.log("Command: lrange bigboxlist 0 -1 | Result: ", commandResult);
/**
* Get list item at index Zero(0)
*
* Command: lindex bigboxlist 0
* Result: "one"
*/
commandResult = await redisClient.lIndex("bigboxlist", 0);
console.log("Command: lindex bigboxlist 0 | Result: " + commandResult);
/**
* Get list item at index One(1)
*
* Command: lindex bigboxlist 1
* Result: "two"
*/
commandResult = await redisClient.lIndex("bigboxlist", 1);
console.log("Command: lindex bigboxlist 1 | Result: " + commandResult);
/**
* Get list item at index Five(5)
*
* Command: lindex bigboxlist 5
* Result: "test a"
*/
commandResult = await redisClient.lIndex("bigboxlist", 5);
console.log("Command: lindex bigboxlist 5 | Result: " + commandResult);
/**
* Get list item at index Negative One(-1)
* The last item in list
*
* Command: lindex bigboxlist -1
* Result: "last item"
*/
commandResult = await redisClient.lIndex("bigboxlist", -1);
console.log("Command: lindex bigboxlist -1 | Result: " + commandResult);
/**
* Get list item at index Negative Two(-2)
* The second last item in list
*
* Command: lindex bigboxlist -2
* Result: "second last item"
*/
commandResult = await redisClient.lIndex("bigboxlist", -2);
console.log("Command: lindex bigboxlist -2 | Result: " + commandResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist 100000000
* Result: (nil)
*/
commandResult = await redisClient.lIndex("bigboxlist", 100000000);
console.log("Command: lindex bigboxlist 100000000 | Result: " + commandResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist -99999999
* Result: (nil)
*/
commandResult = await redisClient.lIndex("bigboxlist", -99999999);
console.log("Command: lindex bigboxlist -99999999 | Result: " + commandResult);
/**
* Try to get list item, when the list does not exist
* Returns (nil)
*
* Command: lindex nonexistingkey 0
* Result: (nil)
*/
commandResult = await redisClient.lIndex("nonexistingkey", 0);
console.log("Command: lindex nonexistingkey 0 | Result: " + commandResult);
/**
* Set a string key
*
* Command: set firststr "some string value here"
* Result: OK
*/
commandResult = await redisClient.set("firststr", "some string value here");
console.log(
'Command: set firststr "some string value here" | Result: ' + commandResult
);
/**
* Try to use LINDEX for an element that is not a list
* We get an error in that case
*
* Command: lindex firststr 0
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.lIndex("firststr", 0);
console.log("Command: lindex firststr 0 | Result: " + commandResult);
} catch (e) {
console.log("Command: lindex firststr 0 | Error: ", e);
}
process.exit(0);
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result: [
'one',
'two',
'three',
'four',
'five',
'test a',
'test b',
'test c',
'second last item',
'last item'
]
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result: null
Command: lindex bigboxlist -99999999 | Result: null
Command: lindex nonexistingkey 0 | Result: null
Command: set firststr "some string value here" | Result: OK
Command: lindex firststr 0 | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Notes
- Use the function “lIndex” from the package node-redis.
// Redis LINDEX command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
public class Lindex {
public static void main(String[] args) {
// Create connection pool
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Create list and push items
*
* Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
* Result: (integer) 10
*/
long pushResult = jedis.rpush("bigboxlist", "one", "two", "three", "four", "five", "test a", "test b", "test c", "second last item", "last item");
System.out.println("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: " + pushResult);
/**
* Check list items
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
* 4) "four"
* 5) "five"
* 6) "test a"
* 7) "test b"
* 8) "test c"
* 9) "second last item"
* 10) "last item"
*/
List<String> lrangeResult = jedis.lrange("bigboxlist", 0, -1);
System.out.println("Command: lrange bigboxlist 0 -1 | Result:");
for (String item: lrangeResult) {
System.out.println(item);
}
/**
* Get list item at index Zero(0)
*
* Command: lindex bigboxlist 0
* Result: "one"
*/
String lindexResult = jedis.lindex("bigboxlist", 0);
System.out.println("Command: lindex bigboxlist 0 | Result: " + lindexResult);
/**
* Get list item at index One(1)
*
* Command: lindex bigboxlist 1
* Result: "two"
*/
lindexResult = jedis.lindex("bigboxlist", 1);
System.out.println("Command: lindex bigboxlist 1 | Result: " + lindexResult);
/**
* Get list item at index Five(5)
*
* Command: lindex bigboxlist 5
* Result: "test a"
*/
lindexResult = jedis.lindex("bigboxlist", 5);
System.out.println("Command: lindex bigboxlist 5 | Result: " + lindexResult);
/**
* Get list item at index Negative One(-1)
* The last item in list
*
* Command: lindex bigboxlist -1
* Result: "last item"
*/
lindexResult = jedis.lindex("bigboxlist", -1);
System.out.println("Command: lindex bigboxlist -1 | Result: " + lindexResult);
/**
* Get list item at index Negative Two(-2)
* The second last item in list
*
* Command: lindex bigboxlist -2
* Result: "second last item"
*/
lindexResult = jedis.lindex("bigboxlist", -2);
System.out.println("Command: lindex bigboxlist -2 | Result: " + lindexResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist 100000000
* Result: (nil)
*/
lindexResult = jedis.lindex("bigboxlist", 100000000);
System.out.println("Command: lindex bigboxlist 100000000 | Result: " + lindexResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist -99999999
* Result: (nil)
*/
lindexResult = jedis.lindex("bigboxlist", -99999999);
System.out.println("Command: lindex bigboxlist -99999999 | Result: " + lindexResult);
/**
* Try to get list item, when the list does not exist
* Returns (nil)
*
* Command: lindex nonexistingkey 0
* Result: (nil)
*/
lindexResult = jedis.lindex("nonexistingkey", 0);
System.out.println("Command: lindex nonexistingkey 0 | Result: " + lindexResult);
/**
* Set a string key
*
* Command: set firststr "some string value here"
* Result: OK
*/
String setResult = jedis.set("firststr", "some string value here");
System.out.println("Command: set firststr "some string value here" | Result: " + setResult);
/**
* Try to use LINDEX for an element that is not a list
* We get an error in that case
*
* Command: lindex firststr 0
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
lindexResult = jedis.lindex("firststr", 0);
System.out.println("Command: lindex firststr 0 | Result: " + lindexResult);
} catch (Exception e) {
System.out.println("Command: lindex firststr 0 | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
test a
test b
test c
second last item
last item
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result: null
Command: lindex bigboxlist -99999999 | Result: null
Command: lindex nonexistingkey 0 | Result: null
Command: set firststr "some string value here" | Result: OK
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lindex” from Jedis package.
- Signature of the method is-
String lindex(final String key, final long index)
// Redis LINDEX command examples in C#
using StackExchange.Redis;
namespace Lindex
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Create list and push items
*
* Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
* Result: (integer) 10
*/
long pushResult = rdb.ListRightPush("bigboxlist", new RedisValue[] { "one", "two", "three", "four", "five", "test a", "test b", "test c", "second last item", "last item" });
Console.WriteLine("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: " + pushResult);
/**
* Check list items
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
* 4) "four"
* 5) "five"
* 6) "test a"
* 7) "test b"
* 8) "test c"
* 9) "second last item"
* 10) "last item"
*/
RedisValue[] lrangeResult = rdb.ListRange("bigboxlist", 0, -1);
Console.WriteLine("Command: lrange bigboxlist 0 -1 | Result:");
foreach (var item in lrangeResult)
{
Console.WriteLine(item);
}
/**
* Get list item at index Zero(0)
*
* Command: lindex bigboxlist 0
* Result: "one"
*/
RedisValue lindexResult = rdb.ListGetByIndex("bigboxlist", 0);
Console.WriteLine("Command: lindex bigboxlist 0 | Result: " + lindexResult);
/**
* Get list item at index One(1)
*
* Command: lindex bigboxlist 1
* Result: "two"
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", 1);
Console.WriteLine("Command: lindex bigboxlist 1 | Result: " + lindexResult);
/**
* Get list item at index Five(5)
*
* Command: lindex bigboxlist 5
* Result: "test a"
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", 5);
Console.WriteLine("Command: lindex bigboxlist 5 | Result: " + lindexResult);
/**
* Get list item at index Negative One(-1)
* The last item in list
*
* Command: lindex bigboxlist -1
* Result: "last item"
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", -1);
Console.WriteLine("Command: lindex bigboxlist -1 | Result: " + lindexResult);
/**
* Get list item at index Negative Two(-2)
* The second last item in list
*
* Command: lindex bigboxlist -2
* Result: "second last item"
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", -2);
Console.WriteLine("Command: lindex bigboxlist -2 | Result: " + lindexResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist 100000000
* Result: (nil)
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", 100000000);
Console.WriteLine("Command: lindex bigboxlist 100000000 | Result: " + lindexResult);
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist -99999999
* Result: (nil)
*/
lindexResult = rdb.ListGetByIndex("bigboxlist", -99999999);
Console.WriteLine("Command: lindex bigboxlist -99999999 | Result: " + lindexResult);
/**
* Try to get list item, when the list does not exist
* Returns (nil)
*
* Command: lindex nonexistingkey 0
* Result: (nil)
*/
lindexResult = rdb.ListGetByIndex("nonexistingkey", 0);
Console.WriteLine("Command: lindex nonexistingkey 0 | Result: " + lindexResult);
/**
* Set a string key
*
* Command: set firststr "some string value here"
* Result: OK
*/
bool setResult = rdb.StringSet("firststr", "some string value here");
Console.WriteLine("Command: set firststr "some string value here" | Result: " + setResult);
/**
* Try to use LINDEX for an element that is not a list
* We get an error in that case
*
* Command: lindex firststr 0
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
lindexResult = rdb.ListGetByIndex("firststr", 0);
Console.WriteLine("Command: lindex firststr 0 | Result: " + lindexResult);
}
catch (Exception e)
{
Console.WriteLine("Command: lindex firststr 0 | Error: " + e.Message);
}
}
}
}
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:
one
two
three
four
five
test a
test b
test c
second last item
last item
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result:
Command: lindex bigboxlist -99999999 | Result:
Command: lindex nonexistingkey 0 | Result:
Command: set firststr "some string value here" | Result: True
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “ListGetByIndex” from StackExchange.Redis.
- Signature of the method is-
RedisValue ListGetByIndex(RedisKey key, long index, CommandFlags flags = CommandFlags.None)
<?php
// Redis LINDEX command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Create list and push items
*
* Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
* Result: (integer) 10
*/
$commandResult = $redisClient->rpush("bigboxlist", [
"one",
"two",
"three",
"four",
"five",
"test a",
"test b",
"test c",
"second last item",
"last item",
]);
echo "Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
| Result: " . $commandResult . "n";
/**
* Check list items
*
* Command: lrange bigboxlist 0 -1
* Result:
* 1) "one"
* 2) "two"
* 3) "three"
* 4) "four"
* 5) "five"
* 6) "test a"
* 7) "test b"
* 8) "test c"
* 9) "second last item"
* 10) "last item"
*/
$commandResult = $redisClient->lrange("bigboxlist", 0, -1);
echo "Command: lrange bigboxlist 0 -1 | Result:n";
print_r($commandResult);
/**
* Get list item at index Zero(0)
*
* Command: lindex bigboxlist 0
* Result: "one"
*/
$commandResult = $redisClient->lindex("bigboxlist", 0);
echo "Command: lindex bigboxlist 0 | Result: " . $commandResult . "n";
/**
* Get list item at index One(1)
*
* Command: lindex bigboxlist 1
* Result: "two"
*/
$commandResult = $redisClient->lindex("bigboxlist", 1);
echo "Command: lindex bigboxlist 1 | Result: " . $commandResult . "n";
/**
* Get list item at index Five(5)
*
* Command: lindex bigboxlist 5
* Result: "test a"
*/
$commandResult = $redisClient->lindex("bigboxlist", 5);
echo "Command: lindex bigboxlist 5 | Result: " . $commandResult . "n";
/**
* Get list item at index Negative One(-1)
* The last item in list
*
* Command: lindex bigboxlist -1
* Result: "last item"
*/
$commandResult = $redisClient->lindex("bigboxlist", -1);
echo "Command: lindex bigboxlist -1 | Result: " . $commandResult . "n";
/**
* Get list item at index Negative Two(-2)
* The second last item in list
*
* Command: lindex bigboxlist -2
* Result: "second last item"
*/
$commandResult = $redisClient->lindex("bigboxlist", -2);
echo "Command: lindex bigboxlist -2 | Result: " . $commandResult . "n";
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist 100000000
* Result: (nil)
*/
$commandResult = $redisClient->lindex("bigboxlist", 100000000);
echo "Command: lindex bigboxlist 100000000 | Result: " . $commandResult . "n";
/**
* Try to get item at index out of index
* Returns (nil), if index is out of range
*
* Command: lindex bigboxlist -99999999
* Result: (nil)
*/
$commandResult = $redisClient->lindex("bigboxlist", -99999999);
echo "Command: lindex bigboxlist -99999999 | Result: " . $commandResult . "n";
/**
* Try to get list item, when the list does not exist
* Returns (nil)
*
* Command: lindex nonexistingkey 0
* Result: (nil)
*/
$commandResult = $redisClient->lindex("nonexistingkey", 0);
echo "Command: lindex nonexistingkey 0 | Result: " . $commandResult . "n";
/**
* Set a string key
*
* Command: set firststr "some string value here"
* Result: OK
*/
$commandResult = $redisClient->set("firststr", "some string value here");
echo "Command: set firststr "some string value here" | Result: " . $commandResult . "n";
/**
* Try to use LINDEX for an element that is not a list
* We get an error in that case
*
* Command: lindex firststr 0
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->lindex("firststr", 0);
echo "Command: lindex firststr 0 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: lindex firststr 0 | Error: " . $e->getMessage() . "n";
}
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => five
[5] => test a
[6] => test b
[7] => test c
[8] => second last item
[9] => last item
)
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result:
Command: lindex bigboxlist -99999999 | Result:
Command: lindex nonexistingkey 0 | Result:
Command: set firststr "some string value here" | Result: OK
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use the method “lindex” of predis.
- Signature of the method is-
lindex(string $key, int $index): string | null
# Redis LINDEX command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Create list and push items
# Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
# Result: (integer) 10
commandResult = redisClient.rpush("bigboxlist",
"one",
"two",
"three",
"four",
"five",
"test a",
"test b",
"test c",
"second last item",
"last item",
)
print("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: {}".format(commandResult))
# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "one"
# 2) "two"
# 3) "three"
# 4) "four"
# 5) "five"
# 6) "test a"
# 7) "test b"
# 8) "test c"
# 9) "second last item"
# 10) "last item"
commandResult = redisClient.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result:{}".format(commandResult))
# Get list item at index Zero(0)
# Command: lindex bigboxlist 0
# Result: "one"
commandResult = redisClient.lindex("bigboxlist", 0)
print("Command: lindex bigboxlist 0 | Result: {}".format(commandResult))
# Get list item at index One(1)
# Command: lindex bigboxlist 1
# Result: "two"
commandResult = redisClient.lindex("bigboxlist", 1)
print("Command: lindex bigboxlist 1 | Result: {}".format(commandResult))
# Get list item at index Five(5)
# Command: lindex bigboxlist 5
# Result: "test a"
commandResult = redisClient.lindex("bigboxlist", 5)
print("Command: lindex bigboxlist 5 | Result: {}".format(commandResult))
# Get list item at index Negative One(-1)
# The last item in list
# Command: lindex bigboxlist -1
# Result: "last item"
commandResult = redisClient.lindex("bigboxlist", -1)
print("Command: lindex bigboxlist -1 | Result: {}".format(commandResult))
# Get list item at index Negative Two(-2)
# The second last item in list
# Command: lindex bigboxlist -2
# Result: "second last item"
commandResult = redisClient.lindex("bigboxlist", -2)
print("Command: lindex bigboxlist -2 | Result: {}".format(commandResult))
# Try to get item at index out of index
# Returns (nil), if index is out of range
# Command: lindex bigboxlist 100000000
# Result: (nil)
commandResult = redisClient.lindex("bigboxlist", 100000000)
print("Command: lindex bigboxlist 100000000 | Result: {}".format(commandResult))
# Try to get item at index out of index
# Returns (nil), if index is out of range
# Command: lindex bigboxlist -99999999
# Result: (nil)
commandResult = redisClient.lindex("bigboxlist", -99999999)
print("Command: lindex bigboxlist -99999999 | Result: {}".format(commandResult))
# Try to get list item, when the list does not exist
# Returns (nil)
# Command: lindex nonexistingkey 0
# Result: (nil)
commandResult = redisClient.lindex("nonexistingkey", 0)
print("Command: lindex nonexistingkey 0 | Result: {}".format(commandResult))
# Set a string key
# Command: set firststr "some string value here"
# Result: OK
commandResult = redisClient.set("firststr", "some string value here")
print("Command: set firststr "some string value here" | Result: {}".format(commandResult))
# Try to use LINDEX for an element that is not a list
# We get an error in that case
# Command: lindex firststr 0
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.lindex("firststr", 0)
print("Command: lindex firststr 0 | Result: {}".format(commandResult))
except Exception as error:
print("Command: lindex firststr 0 | Error: ", error)
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:['one', 'two', 'three', 'four', 'five', 'test a', 'test b', 'test c', 'second last item', 'last item']
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result: None
Command: lindex bigboxlist -99999999 | Result: None
Command: lindex nonexistingkey 0 | Result: None
Command: set firststr "some string value here" | Result: True
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lindex” from redis-py.
- Signature of the method is –
def lindex(self, name: str, index: int) -> Union[Awaitable[Optional[str]], Optional[str]]
# Redis LINDEX command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Create list and push items
# Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item"
# Result: (integer) 10
commandResult = redis.rpush("bigboxlist", [
"one",
"two",
"three",
"four",
"five",
"test a",
"test b",
"test c",
"second last item",
"last item",
])
print("Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: ", commandResult, "n")
# Check list items
# Command: lrange bigboxlist 0 -1
# Result:
# 1) "one"
# 2) "two"
# 3) "three"
# 4) "four"
# 5) "five"
# 6) "test a"
# 7) "test b"
# 8) "test c"
# 9) "second last item"
# 10) "last item"
commandResult = redis.lrange("bigboxlist", 0, -1)
print("Command: lrange bigboxlist 0 -1 | Result:", commandResult, "n")
# Get list item at index Zero(0)
# Command: lindex bigboxlist 0
# Result: "one"
commandResult = redis.lindex("bigboxlist", 0)
print("Command: lindex bigboxlist 0 | Result: ", commandResult, "n")
# Get list item at index One(1)
# Command: lindex bigboxlist 1
# Result: "two"
commandResult = redis.lindex("bigboxlist", 1)
print("Command: lindex bigboxlist 1 | Result: ", commandResult, "n")
# Get list item at index Five(5)
# Command: lindex bigboxlist 5
# Result: "test a"
commandResult = redis.lindex("bigboxlist", 5)
print("Command: lindex bigboxlist 5 | Result: ", commandResult, "n")
# Get list item at index Negative One(-1)
# The last item in list
# Command: lindex bigboxlist -1
# Result: "last item"
commandResult = redis.lindex("bigboxlist", -1)
print("Command: lindex bigboxlist -1 | Result: ", commandResult, "n")
# Get list item at index Negative Two(-2)
# The second last item in list
# Command: lindex bigboxlist -2
# Result: "second last item"
commandResult = redis.lindex("bigboxlist", -2)
print("Command: lindex bigboxlist -2 | Result: ", commandResult, "n")
# Try to get item at index out of index
# Returns (nil), if index is out of range
# Command: lindex bigboxlist 100000000
# Result: (nil)
commandResult = redis.lindex("bigboxlist", 100000000)
print("Command: lindex bigboxlist 100000000 | Result: ", commandResult, "n")
# Try to get item at index out of index
# Returns (nil), if index is out of range
# Command: lindex bigboxlist -99999999
# Result: (nil)
commandResult = redis.lindex("bigboxlist", -99999999)
print("Command: lindex bigboxlist -99999999 | Result: ", commandResult, "n")
# Try to get list item, when the list does not exist
# Returns (nil)
# Command: lindex nonexistingkey 0
# Result: (nil)
commandResult = redis.lindex("nonexistingkey", 0)
print("Command: lindex nonexistingkey 0 | Result: ", commandResult, "n")
# Set a string key
# Command: set firststr "some string value here"
# Result: OK
commandResult = redis.set("firststr", "some string value here")
print("Command: set firststr "some string value here" | Result: ", commandResult, "n")
# Try to use LINDEX for an element that is not a list
# We get an error in that case
# Command: lindex firststr 0
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.lindex("firststr", 0)
print("Command: lindex firststr 0 | Result: ", commandResult, "n")
rescue => error
print("Command: lindex firststr 0 | Error: ", error, "n")
end
Output:
Command: rpush bigboxlist one two three four five "test a" "test b" "test c" "second last item" "last item" | Result: 10
Command: lrange bigboxlist 0 -1 | Result:["one", "two", "three", "four", "five", "test a", "test b", "test c", "second last item", "last item"]
Command: lindex bigboxlist 0 | Result: one
Command: lindex bigboxlist 1 | Result: two
Command: lindex bigboxlist 5 | Result: test a
Command: lindex bigboxlist -1 | Result: last item
Command: lindex bigboxlist -2 | Result: second last item
Command: lindex bigboxlist 100000000 | Result:
Command: lindex bigboxlist -99999999 | Result:
Command: lindex nonexistingkey 0 | Result:
Command: set firststr "some string value here" | Result: OK
Command: lindex firststr 0 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Notes
- Use method “lindex” from the redis-rb.
- Signature of the method is-
# @param [String] key
# @param [Integer] index
# @return [String]
def lindex(key, index)
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 |
---|---|
LPUSH | Command Details |
RPUSH | Command Details |
LRANGE | Command Details |