LINDEX command in redis

LINDEX key index

This command returns the specified element of the list being stored at the specified key. 0 is the first element, 1 is the second, and so on.

Note: Negative indexes are supported, for example, -1 is the last element, -2 is the penultimate, and so on.


TIME COMPLEXITY: O(n) (with n being the length of the list)

If the value stored at key is not of list type an error is returned. If the index is out of range an empty string is returned.

Note that even if the average time complexity is O(n) asking for the first or the last element of the list is O(1).

RETURN VALUE: Returns the value at the given index of the key.

Syntax:

LINDEX KEY_NAME INDEX

Example 1: When the value at the index is present

redis:6379> LPUSH myList 100
(integer) 1
redis:6379> LINDEX myList 0
"100"

Output

100


Example 2: When the value at the index is not present

redis:6379> LINDEX myList 1
(nil)

Output

nil


No comments:

Post a Comment